Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

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

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

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

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.

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.

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.

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

This is using a UIWebView in a native iPhone app.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One workaround of this problem as I see is to

1) download HTML code

2) store it in the string

3) find all external links in it like

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

4) download them all

5) replace them with embedded Base64-encoded version

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...