본문 바로가기
모바일개발(Mobile Dev)/IOS개발(ObjectC)

How to clear UIWebView cache

by 테크한스 2015. 12. 12.
반응형

written by http://nscookbook.com/2013/03/ios-programming-recipe-16-populating-a-uitableview-with-data-from-the-web/


I need to show a UIWebView that opens a local html, fetching from a remote server. Each time, the UIWebView shows incorrect data, because the iOS caching system.

I did many attempts to solve this problem, but it does not work:

1) Programmatically add UIWebView when the UIViewController starts loading. After the UIViewController disappears, stop loading the UIWebView, and release the UIWebView.

2) Ignore the local cache data:

NSURL *websiteUrl = [NSURL fileURLWithPath:localHtmlPath];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:websiteUrl];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[self.webView loadRequest:request];

3) Clear the cache when the UIViewController disappears

[[NSURLCache sharedURLCache] removeAllCachedResponses];
    for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {

        if([[cookie domain] isEqualToString:localHtmlPath]) {

            [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
        }
    }

4) Disable the cache

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
    [NSURLCache setSharedURLCache:sharedCache];
}

5) Reduce iOS memory utilization by taming NSURLCache

follow the tutorial link

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    int cacheSizeMemory = 4*1024*1024; // 4MB
    int cacheSizeDisk = 32*1024*1024; // 32MB
    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
    [NSURLCache setSharedURLCache:sharedCache];
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}

6) Follow this link to prevent CSS caching.

None of these above methods works. Any workaround?

2 similar answers  β 

By Joge

Try this may be help full ....

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

Thanks & cheers

By Sharanya K M

You seem to have tried almost everything. I'm aware you are rejecting the cookies. Yet, Why dont you try this? This worked for me...

   NSHTTPCookie *aCookie;
    for (aCookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:aCookie];
    }


반응형