0

I am using Fabric SDK for Twitter login into my app.

I want to remove session and cookie of Twitter from my iOS App. Because I am logging 1st time successfully from Twitter using this SDK. But if user wants to login from other credentials using Twitter into my app, then its not possible without clear session.

For clear session of Twitter, I am using following Code. But its not working.

[[Twitter sharedInstance]logOut];
[[Twitter sharedInstance]logOutGuest];
NSHTTPCookieStorage *cookieList = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *x in cookieList.cookies)
{
     if ([[x valueForKey:@"domain"] isEqualToString:@".twitter.com"])
     {
           [cookieList deleteCookie:x];
     }
}

Look forward to hearing your responses!

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81

2 Answers2

1

Try adding this to remove cookies

    let cookie = NSHTTPCookie.self
    let cookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()

    for cookie in cookieStorage.cookies!
    {
       cookieStorage.deleteCookie(cookie)
    }

In Objective-C

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *httpCookie in cookieStorage.cookies)

{
    if ([[httpCookie valueForKey:@"domain"] isEqualToString:@".twitter.com"])
    {
        [cookieStorage deleteCookie:httpCookie];
    }
}
Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73
  • Please share this code in objective-c language. Is this correct?? NSHTTPCookie * cookie = NSHTTPCookie.self; NSHTTPCookieStorage *cookieList = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (cookie in cookieList.cookies) { [cookieList deleteCookie:cookie]; } – Meet Doshi Nov 16 '15 at 06:31
  • you can check this link for objective-c http://stackoverflow.com/a/21823068/5362916 – Uma Madhavi Nov 16 '15 at 06:37
  • Thanksss !!! I am trying this, but its not working for me. because I want to remove session of Twitter form my app. – Meet Doshi Nov 16 '15 at 07:12
  • also you can use like this NSURL *url = [NSURL URLWithString:@"https://api.twitter.com"]; NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url]; for (NSHTTPCookie *cookie in cookies) { [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; } – Uma Madhavi Nov 16 '15 at 07:21
1

Did you use breakpoints in order to know if it is entering inside de if condition?

Btw, you have to change the code, you can't delete an object from the array while you are enumerating it. Maybe you can use another variable to persist the cookie when you find it and remove it out of the forin loop

// EDIT

NSHTTPCookieStorage *cookieList = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSMutableArray *cookies = [[NSMutableArray alloc] init];
for (NSHTTPCookie *x in cookieList.cookies)
{
     if ([[x valueForKey:@"domain"] isEqualToString:@".twitter.com"])
     {
         [cookies addObject:x];
     }
}
if (cookies.count > 0) {
    for (NSHTTPCookie *cookie in cookies) {
        [cookieList deleteCookie:cookie];
    }
}

I created an array because I don't know how many cookies persist from twitter

Drizztneko
  • 161
  • 7
  • Yess, I debugging this using breakpoints. I know that's the issue. but I don't have any alternate solution right now. So If you have any alternate solution, then please share to me. I want to clear session and cookie of Twitter from my App. – Meet Doshi Nov 16 '15 at 06:47
  • Maybe the domain is not exactly ".twitter.com", did you try to print the whole array? This way you can search it and write it correctly (if it was wrong) – Drizztneko Nov 16 '15 at 06:51
  • Yess I print whole array. And sometimes it has 4 objects or sometimes 7 objects. Also i am trying to @"twitter" as domain name. Its working same. but not solving my problem. – Meet Doshi Nov 16 '15 at 07:01
  • the only thing than I can suggest is the way of delete the cookies but i can't help you founding that cookie because i've never work with them. I will edit my answer with the code. – Drizztneko Nov 16 '15 at 07:11