【问题标题】:How to delete the cache in UIWebview?如何删除 UIWebview 中的缓存?
【发布时间】:2017-02-03 11:02:10
【问题描述】:

我已经在webview 中加载了 url,加载是成功的,但是每当我再次点击它在缓存中显示两个 url 和再次显示三个 url 等时,这意味着它继续维护堆栈。我使用了很多方法,但无法正常工作。

这些都是我到现在为止的尝试。

1.

[[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest];


[[NSURLCache sharedURLCache] removeAllCachedResponses];

for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {

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

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

2.

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];

3.

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

4.

-(void) viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];

[self.webView removeFromSuperview];
self.webView.delegate = nil;
self.webView = nil;
}

5.

[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];

6.

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.webView stopLoading];
}
- (void) stopLoading {
[self.webView stopLoading];
self.webView.delegate = nil;
NSBundle *mainBundle = [NSBundle mainBundle];
NSURL *homeIndexUrl = [mainBundle URLForResource:@"Home" withExtension:@"html"];
NSURLRequest *urlReq = [NSURLRequest requestWithURL:homeIndexUrl];
[self.webView loadRequest:urlReq];
[EALoaderView hideLoaderForView:self.view animated:YES];
[self.webView loadHTMLString:html baseURL:nil];
 }

我也尝试过以下这些答案。

1.Clearing UIWebview cache

2.NSURLProtocol isn't asked to load after YES response to canInitWithRequest

3.How To Clear A UIWebView

4.How to delete the cache from UIWebview or dealloc UIWebview

5.How to clear UIWebView cache?

6.UIwebview without Cache

我做了很多研发,但没有找到解决这些问题的确切解决方案。

谢谢。

【问题讨论】:

    标签: ios objective-c caching uiwebview


    【解决方案1】:

    我使用下面的代码删除缓存,希望它对你有用:

    //This must be called in Main Thread
    + (void)clearWebCache
    {
     //http://stackoverflow.com/questions/31289838/how-to-delete-wkwebview-cookies
        //https://github.com/ShingoFukuyama/WKWebViewTips
        //http://stackoverflow.com/questions/27105094/how-to-remove-cache-in-wkwebview/32491271#32491271
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")) {
            NSSet *websiteDataTypes = [NSSet setWithArray:@[WKWebsiteDataTypeDiskCache,
                                                            //WKWebsiteDataTypeOfflineWebApplicationCache,
                                                            WKWebsiteDataTypeMemoryCache,
                                                            //WKWebsiteDataTypeLocalStorage,
                                                            //WKWebsiteDataTypeCookies,
                                                            //WKWebsiteDataTypeSessionStorage,
                                                            //WKWebsiteDataTypeIndexedDBDatabases,
                                                            //WKWebsiteDataTypeWebSQLDatabases
                                                            ]];
            //// All kinds of data
            //            NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
            //// Date from
            NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
            //// Execute
            [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
                // Done
            }];
        }
        else {
            NSHTTPCookie *cookie;
            NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
            for (cookie in [storage cookies]) {
                [storage deleteCookie:cookie];
            }
            [[NSURLCache sharedURLCache] removeAllCachedResponses];
        }
    }

    【讨论】:

      【解决方案2】:

      嘿,伙计们! 研究了这个主题一段时间,并找到了最适合我的解决方案 - 使用 WKWebView 它不会泄漏! 就是这样 - 如此简单。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-09
        • 2011-02-01
        • 1970-01-01
        • 2011-07-25
        • 2013-04-16
        • 2015-04-13
        • 1970-01-01
        • 2015-03-22
        相关资源
        最近更新 更多