gpt4 book ai didi

ios - 如何删除UIWebview中的缓存?

转载 作者:行者123 更新时间:2023-12-01 17:04:42 27 4
gpt4 key购买 nike

我已经将URL加载到webview中,加载成功,但是每当我再次在缓存中命中它的show 2 url,再一次它的show 3 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

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

谢谢。

最佳答案

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

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

关于ios - 如何删除UIWebview中的缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39697122/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com