是否可以缓存 iPhone UIWebView 中加载的资源? [英] Is it possible to cache resources loaded in an iPhone UIWebView?

查看:29
本文介绍了是否可以缓存 iPhone UIWebView 中加载的资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的应用程序,在 UIWebView 中加载了一个针对 iPhone 优化的网站.

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

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

Problem is, caching does not seem to work:

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

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

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.)

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

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.

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

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.

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

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 研究是从大约一年前开始的,我猜测只有移动 safari.

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

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

This is using a UIWebView in a native iPhone app.

推荐答案

据我所知,此问题的一种解决方法是

One workaround of this problem as I see is to

1) 下载 HTML 代码

1) download HTML code

2) 将其存储在字符串中

2) store it in the string

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

3) find all external links in it like

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

4) 全部下载

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

5) replace them with embedded Base64-encoded version

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

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

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

这篇关于是否可以缓存 iPhone UIWebView 中加载的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆