Vineeth N K
Vineeth N K

Reputation: 51

How to clear cache and cookies in iphone

In my iPhone application I want to clear the cache and cookies at the logout page. Please give proper suggestions for that. Now I am using the below code, but it is not working properly.

[[NSURLCache sharedURLCache] removeAllCachedResponses];

Upvotes: 2

Views: 2412

Answers (1)

iNoob
iNoob

Reputation: 3364

I've been able to make it work using NSHTTPCookie and NSHTTPCookieStorage

    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies])
    {
        NSString* domainName = [cookie domain];
        NSRange domainRange = [domainName rangeOfString:@"facebook"]; //i used this to remove facebook related cookie so gave the domain name as facebook
        if(domainRange.length > 0)
        {
            [storage deleteCookie:cookie];
        }
    }

I've never actually worked with clearing cache, but used a piece of code which disables caching

NSURLCache *disableCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0      diskPath:nil];
[NSURLCache setSharedURLCache:disableCache]; 
[disableCache release];

Upvotes: 5

Related Questions