美文网首页
UIWebView完美的清空缓存

UIWebView完美的清空缓存

作者: Tywin_Zhang | 来源:发表于2016-03-24 16:07 被阅读1871次

删除cache文件夹是最好的清空缓存的方式:

- (void)clearAllUIWebViewData {
    // Clear the webview cache...
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    [self removeApplicationLibraryDirectoryWithDirectory:@"Caches"];
    [self removeApplicationLibraryDirectoryWithDirectory:@"WebKit"];
    // Empty the cookie jar...
    for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
    [selfremoveApplicationLibraryDirectoryWithDirectory:@"Cookies"];
}

- (void)removeApplicationLibraryDirectoryWithDirectory:(NSString *)dirName {
    NSString *dir = [[[[NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES) lastObject]stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Library"] stringByAppendingPathComponent:dirName];
    if ([[NSFileManager defaultManager] fileExistsAtPath:dir]) {
      [[NSFileManager defaultManager] removeItemAtPath:dir error:nil];
    }
}

相关文章

网友评论

      本文标题:UIWebView完美的清空缓存

      本文链接:https://www.haomeiwen.com/subject/czyrlttx.html