ios 没有缓存的 UIWebview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9744644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:22:23  来源:igfitidea点击:

UIWebview without Cache

iphoneios

提问by Jean St John

I want to load a webview without cache?

我想加载一个没有缓存的 webview?

//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[kdgWebView loadRequest:requestObj];

回答by LuisEspinoza

Objective-C:

目标-C:

// Remove and disable all URL Cache, but doesn't seem to affect the memory
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];

You can find more information in http://blog.techno-barje.fr/post/2010/10/04/UIWebView-secrets-part2-leaks-on-release/

您可以在http://blog.techno-barje.fr/post/2010/10/04/UIWebView-secrets-part2-leaks-on-release/ 中找到更多信息

Swift:

迅速:

URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0

回答by LuisEspinoza

You can change your cache policy, using the code:

您可以使用以下代码更改缓存策略:

NSURLRequest *requestObj = [NSURLRequest requestWithURL:url 
                                        cachePolicy:NSURLRequestReloadIgnoringCacheData
                                    timeoutInterval: 10.0]; 

回答by jgorozco

I try to answer a similar question here. I hope it was useful for you! The last solution works for HTML but not for images, css and js linked in html.

我试着在这里回答一个类似的问题。我希望它对你有用!最后一个解决方案适用于 HTML,但不适用于 html 中链接的图像、css 和 js。

How to clear UIWebView cache?

如何清除 UIWebView 缓存?

回答by Jim True

I've tried all of these solutions but none of them work every single time. My main test was an html page with an image set as a background in the html page. This html page and image are downloaded locally on my iPad through a "sync" method i have written. The only way to keep the cache fully reset is the constantly change the url of the image in the html page so it thinks it's a unique image each time. For example in my html:

我已经尝试了所有这些解决方案,但没有一个每次都有效。我的主要测试是一个 html 页面,在 html 页面中将图像设置为背景。这个 html 页面和图像是通过我编写的“同步”方法在我的 iPad 上本地下载的。保持缓存完全重置的唯一方法是不断更改 html 页面中图像的 url,因此它每次都认为它是唯一的图像。例如在我的 html 中:

<body background="image.png?v123">

So each page change i make i have to update the version number:

因此,我所做的每个页面更改都必须更新版本号:

<body background="image.png?v1234">

etc, etc. You can change the url in anyway you want, it just has to be different.

等等,等等。您可以随意更改网址,只是必须有所不同。

Well i'm tired of doing this. Now When i download content locally, I am instead dynamically changing the containing folder's name. Since this folder is a parent folder and all of my links are relative it means i don't have to edit my html files each time. So far it seems to be working ok. I wish these other methods were consistent as it would be so much easier to simply reset the cache.

好吧,我厌倦了这样做。现在,当我在本地下载内容时,我会动态更改包含文件夹的名称。由于该文件夹是父文件夹和我所有的链接都是相对的,那就意味着我不必修改我的HTML每一次文件。到目前为止,它似乎工作正常。我希望这些其他方法是一致的,因为简单地重置缓存会容易得多。

So in my requests its the baseURL which is causing my cache to reset, not the query string to my url or background image, or css file, etc, etc.

因此,在我的请求中,它是导致我的缓存重置的 baseURL,而不是我的 url 或背景图像或 css 文件等的查询字符串。

NSString *myURL = @"index.html";
NSURL *baseURL = [NSURL fileURLWithPath:@"FULL_PATH_TO_DOCS/MYFOLDERV123"];
[myTargetWebView loadHTMLString:myURL baseURL:baseURL];