ios 是否可以缓存 iPhone UIWebView 中加载的资源?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/345432/
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 15:25:44  来源:igfitidea点击:

Is it possible to cache resources loaded in an iPhone UIWebView?

iosiphonecocoa-touchcachinguiwebview

提问by kevin

I have a simple app loading a site optimized for the iPhone in a UIWebView.

我有一个简单的应用程序,在UIWebView.

Problem is, caching does not seem to work:

问题是,缓存似乎不起作用:

[webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: url]
                                       cachePolicy: NSURLRequestUseProtocolCachePolicy
                                   timeoutInterval: 60.0]];

Any things referenced in this remote page (css, images, external javascript files) never get cached (the requests never send a If-Modified-Since header or anything else in the way of cache control.)

此远程页面中引用的任何内容(css、图像、外部 javascript 文件)永远不会被缓存(请求永远不会发送 If-Modified-Since 标头或任何其他缓存控制方式。)

Is it possible? It seems with a regular Cocoa WebView there a delegate methods that get called for each resource request and post load (-didFinishLoadingFromDataSource:) which you could use to roll your own caching.. but that does not seem applicable here.

是否可以?对于常规的 Cocoa WebView,似乎有一个委托方法可以为每个资源请求和后加载 ( -didFinishLoadingFromDataSource:)调用,您可以使用它来滚动自己的缓存..但这在这里似乎不适用。

My entire page (page and its referenced resources) is around 89K compressed.. which is slow over 3G in some spots and even worse over EDGE. Incoming requests are at least indicating that it accepts compression (accept-encoding=gzip, deflate), so that's good I suppose.

我的整个页面(页面及其引用的资源)大约是 89K 压缩的……这在某些地方比 3G 慢,比 EDGE 更糟。传入的请求至少表明它接受压缩 ( accept-encoding=gzip, deflate),所以我想这很好。

I read this yui study, which seems to indicate that the iPhone will cache 25k per item. The only thing referenced that is over 25k uncompressed is jquery (packed but uncompressed - it is 30k). Everything else should be cacheable. No request for anything referenced in the page fetched is triggering a 304 on the server side.

我读了这个 yui study,它似乎表明 iPhone 将缓存 25k 每个项目。唯一引用的未压缩超过 25k 的内容是 jquery(已打包但未压缩 - 它是 30k)。其他一切都应该是可缓存的。对获取的页面中引用的任何内容的请求都不会在服务器端触发 304。

That yui study was from almost a year ago, and I am guessing with mobile safari only.

那个 yui 研究是从大约一年前开始的,我猜测只有移动 safari。

This is using a UIWebViewin a native iPhone app.

这是UIWebView在本机 iPhone 应用程序中使用的。

回答by Denis

One workaround of this problem as I see is to

我所看到的这个问题的一种解决方法是

1) download HTML code

1)下载HTML代码

2) store it in the string

2)将其存储在字符串中

3) find all external links in it like

3)找到其中的所有外部链接,例如

<img src="img.gif" width="..." height="..." />

4) download them all

4)全部下载

5) replace them with embedded Base64-encoded version

5) 用嵌入的 Base64 编码版本替换它们

<img src="data:image/gif;base64,R0lGODlhUAAPA...JADs= " width="..." height="..." />

6) finally store complete HTML with embedded images as you want.

6)最后根据需要存储带有嵌入图像的完整HTML。

回答by RandyMcMillan

From https://github.com/phonegap/phonegap-iphone/issues/148:

https://github.com/phonegap/phonegap-iphone/issues/148

NSURLCache* cache = [NSURLCache sharedURLCache];
[cache setMemoryCapacity:4 * 1024 * 1024];
[cache setDiskCapacity:512*1024];

[NSURLRequest requestWithURL:appURL
                 cachePolicy:NSURLRequestReturnCacheDataElseLoad
             timeoutInterval:10.0];

回答by rage

You can now try ASIWebPageRequestby All Seeing Interactive:

您现在可以ASIWebPageRequest通过 All Seeing Interactive尝试:

ASIWebPageRequest is a new experimental addition to the ASIHTTPRequest family. It can be used to download a complete webpage, including external resources like images and stylesheets, in a single request. Once a web page is downloaded, the request will parse the content, look for external resources, download them, and insert them directly into the html source using Data URIS. You can then take the response and put it directly into a UIWebView / WebView on Mac.

ASIWebPageRequest 是 ASIHTTPRequest 系列的新实验性补充。它可用于在单个请求中下载完整的网页,包括图像和样式表等外部资源。下载网页后,请求将解析内容,查找外部资源,下载它们,然后使用 Data URIS 将它们直接插入到 html 源中。然后,您可以获取响应并将其直接放入 Mac 上的 UIWebView/WebView。

I can only advise everyone to use Ben Copsey's great library for all sorts of HTTP operations anyways.

无论如何,我只能建议每个人使用 Ben Copsey 的优秀库进行各种 HTTP 操作。

UPDATE: Ben has discontinued ASIHTTPRequest. I no longer suggest using it.

更新:Ben 已停止使用 ASIHTTPRequest。我不再建议使用它。

回答by rage

You can always perform the requests manually, though that'll be tricky - and then you can cache things to your heart's content. Build a UIWebViewDelegatethat starts the request in webView:shouldStartLoadWithRequest:navigationType:, cache the result, and use the UIWebView's loadHTMLString:baseURL:to update the view.

您始终可以手动执行请求,尽管这会很棘手 - 然后您可以将内容缓存到您的内心深处。构建在UIWebViewDelegate中启动请求webView:shouldStartLoadWithRequest:navigationType:,缓存结果,并使用 UIWebViewloadHTMLString:baseURL:更新视图。

It'll be ugly, and things won't work as smoothly as you might want, but it may be good enough for what you need.

它会很丑,事情不会像你想要的那样顺利,但它可能足以满足你的需要。

回答by seanb

The ihone has limited caching capacity compared to a normal computer. It limits uncompressedcache items to 25k.

与普通计算机相比,ihone 的缓存容量有限。它将未压缩的缓存项限制为 25k。

Good info here: http://yuiblog.com/blog/2008/02/06/iphone-cacheability/

这里的好信息:http: //yuiblog.com/blog/2008/02/06/iphone-cacheability/

回答by Palimondo

You should be able to subclass NSURLCacheand substitute it for the shared cache used by the UIWebViewas described in this Cocoa with Love article: Substituting local data for remote UIWebView requests

您应该能够子类化NSURLCache并将其替换为UIWebViewCocoa with Love 文章中所述的共享缓存:为远程 UIWebView 请求替换本地数据

For another approach have a look at Drop-in offline caching for UIWebView (and NSURLProtocol).

对于另一种方法,请查看UIWebView (和 NSURLProtocol) 的 Drop-in offline caching