0

I'm building an iOS app in which i have implemented Fabric and using Digits for login.When login is successful session remains open.

I'm facing an issue i want to clear the app cache to reset the session so user can login again with a new phone number.But it is not happening.

The code i'm using to clear the app cache:

- (void)viewDidLoad {

    [super viewDidLoad];

    [[NSURLCache sharedURLCache] removeAllCachedResponses];

}

Help is appreciated!

Daljeet
  • 1,573
  • 2
  • 20
  • 40

2 Answers2

4

Since some crucial parts of your problem is missing (like what authentication method you use), I'll just post a handy snippet here that MIGHT resolve your problem.

BUT BEWARE: This WILL remove all cookies and all cached responses along with all NSURLCredentials (as long as they are not persisted).

- (void)removeAllStoredCredentials
{
    // Delete any cached URLrequests!
    NSURLCache *sharedCache = [NSURLCache sharedURLCache];
    [sharedCache removeAllCachedResponses];

    // Also delete all stored cookies!
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *cookies = [cookieStorage cookies];
    id cookie;
    for (cookie in cookies) {
        [cookieStorage deleteCookie:cookie];
    }

    NSDictionary *credentialsDict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials];
    if ([credentialsDict count] > 0) {
        // the credentialsDict has NSURLProtectionSpace objs as keys and dicts of userName => NSURLCredential
        NSEnumerator *protectionSpaceEnumerator = [credentialsDict keyEnumerator];
        id urlProtectionSpace;
        // iterate over all NSURLProtectionSpaces
        while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) {
            NSEnumerator *userNameEnumerator = [[credentialsDict objectForKey:urlProtectionSpace] keyEnumerator];
            id userName;
            // iterate over all usernames for this protectionspace, which are the keys for the actual NSURLCredentials
            while (userName = [userNameEnumerator nextObject]) {
                NSURLCredential *cred = [[credentialsDict objectForKey:urlProtectionSpace] objectForKey:userName];
                //NSLog(@"credentials to be removed: %@", cred);
                [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:cred forProtectionSpace:urlProtectionSpace];
            }
        }
    } 
}
Widerberg
  • 1,118
  • 1
  • 10
  • 24
  • Thanks @Alexander for reply...but its not working even this is not clear my NSUserDefaults. – Daljeet Feb 24 '15 at 13:21
  • Yes, seems @Clafou did understand the question when i didn't. My snippet will work on low-level abstractions lying on top of NSURCache or that use NSURLCredentials. – Widerberg Feb 24 '15 at 14:09
  • Yes you are right Alexander any ways thanks for your suggestions. – Daljeet Feb 24 '15 at 14:10
2

Rather than go low-level and mess with NSURLCache, you can use your library's own high-level mechanics:

[[Digits sharedInstance] logOut]
Clafou
  • 15,250
  • 7
  • 58
  • 89