How to clear UIWebView cache?

前端 未结 9 1779
闹比i
闹比i 2020-11-29 04:47

I need UIWebView to display some local .webarchive file. But images there have same names, so UIWebView shows only one image all the time. How can I clear the cache?

9条回答
  •  我在风中等你
    2020-11-29 05:05

    I'm working with some designers editing the CSS files used by some web views in an app. They were complaining that changes to the stylesheets weren't being reflected in the app, and a bit of debugging with Charles confirmed that they weren't being reloaded. I tried seemingly every answer on StackOverflow, to no avail.

    What finally did the trick was creating an NSURLCache subclass that overrides -cachedResponseForRequest:

    - (NSCachedURLResponse*)cachedResponseForRequest:(NSURLRequest*)request
    {
        if ([[[[request URL] absoluteString] pathExtension] caseInsensitiveCompare:@"css"] == NSOrderedSame)
            return nil;
        else
            return [super cachedResponseForRequest:request];
    }
    

    I then install it with a reasonable memory and disk capacity:

    NSURLCache *currentCache = [NSURLCache sharedURLCache];
    NSString *cachePath = [cachesDirectory() stringByAppendingPathComponent:@"DebugCache"];
    
    DebugURLCache *cache = [[DebugURLCache alloc] initWithMemoryCapacity:currentCache.memoryCapacity diskCapacity:currentCache.diskCapacity diskPath:cachePath];
    [NSURLCache setSharedURLCache:cache];
    [cache release];
    

    (cachesDirectory() is a function that returns /Library/Caches under the application directory on iOS).

    As you can tell by the name, I'm using this only in the debug configuration (using the Additional Preprocessor Flags build setting and some #ifdefs).

提交回复
热议问题