How to Cache Resources Loaded in an iPhone Uiwebview

Need content in UIWebView to display quickly

Edit: Just pointing out that this question, and its answers, are 4 years old. I'm chatting about iOS 3.x below. I can easily imagine that things have changed in the meantime -- heck, the devices alone are faster than the iPhone 3G was. YMMV. :)

I've been looking at this myself for the past while, not actually implementing anything yet, just Googling. It seems overall that it's very hard / impossible to do this on the iPhone using DontLoad. Even in March, there were reports that it works in Simulator, but not on device: http://discussions.apple.com/message.jspa?messageID=9245292

I'm just guessing, but based on even other StackOverflow questions (e.g. this one) I'm thinking perhaps the on-device caching is somehow more limited than both its Mac and Safari counterparts.

Others on the Apple forums report experiences with rude engineers when asking about UIWebView and NSURLCache, where in bug reports the engineers say it should work, but actual developers say it doesn't. http://discussions.apple.com/thread.jspa?threadID=1588010 (Jul 29-Aug 20)

Some solutions might be found here. Considering [UIImage imageWithData:...], David describes some code for "an implementation of an asynchronous, caching image downloader for iPhone."

And now I've discovered this Aug 10th email on the cocoa-dev mailing list:

On Aug 10, 2009, at 1:38 PM, Mike Manzano wrote:

Has anyone been able to successfully
get the URL loading system on iPhone
to pay attention to custom versions of
NSURLCache? It seems to me that it's
being ignored. More info here:


[a link to this page; removed.]


(See the second "answer" on that
page).


Just need some sort of clue if I'm
doing something wrong or if my code's
just being ignored.


Yes, I've had it work - it just
requires making your own cache
instance and explicitly setting the
shared cache to that new instance.

But it absolutely did work - once
implemented, my "set the table view
cell's image to an image from the net"
works nicely (and without it scrolling
was painful at best).

Glenn Andreas gandreas@xxxxxxxxxxxx
http://www.gandreas.com/ wicked fun!
Mad, Bad, and Dangerous to Know

The above email can be found at http://www.cocoabuilder.com/archive/message/cocoa/2009/8/10/242484 (with no further replies)

Further proof that NSURLCache can work is found in iCab Blog: URL filtering for UIWebView on the iPhone (Aug 18, 2009)

Anyone here probably should also look to Apple's URLCache sample app as it may be relevant to their work: https://developer.apple.com/iphone/library/samplecode/URLCache/index.html Except having just looked at it now, it says "This application does not use a NSURLCache disk or memory cache, so our cache policy is to satisfy the request by loading the data from its source." and uses cachePolicy:NSURLRequestReloadIgnoringLocalCacheData. So this is less relevant than I thought, and why this person was calling NSURLCache a joke.

So it seems NSURLCache is not as tricky or impossible as I started this post by saying. Gotta love finding a way that works while researching reasons it won't work. I still can't believe there are only 25 Google search results for "iphone" and "NSURLRequestReturnCacheDataDontLoad"... This answer has just about every one of em. And I'm glad I wrote it so I can refer to it later ;-)

UIWebView Copy Cache Data For Offline View

You may used ASIWebPageRequest , example given here

Or

If reachability got connectivity

NSCachedURLResponse* response = [[NSURLCache sharedURLCache] 
cachedResponseForRequest:[webView request]];
NSData* data = [response data];

Store data in locally and use it whenever you got reachability offline.



Related Topics



Leave a reply



Submit