UIWebView webpage caching for offline viewing
Asked Answered
V

2

20

First of all, I'm pretty sure that I have checked every answer here and nothing does what I would like to do.

  1. In this question, for answer is given ASIHTTPRequest which is dead project. (How do I download an entire webpage (with images) on the iPhone?)
  2. In this question, user proposed RNCachingURLProtocol which is really great but I had a few problems after closing app completely (closing it in task-bar). After that I didn't get css or images, only html was loaded. (Cache a single webpage for use when offline in Xcode / UIWEBVIEW).

There are few more answers but none is good. There must be some simple implementation for what I'm searching.

I would like to: When app opens, it loads some webpage. I want to save that webpage completely. Now user can quit or do whatever he wants (just not uninstall). As long as there is some internet connection (I check that using reachability class), webpage loads normally and it's being saved. IF USER opens app and there is NO INTERNET connection I just want to show message that "it might not be up to date bla bla boa" and show complete, saved webpage that was saved last time application has internet connection.

What would be the best way (up to date) to save complete webpage. I'v found something about MKNetworkKit but I'm not sure how to use that. Any help would be appreciated.

Vankirk answered 30/5, 2013 at 2:5 Comment(4)
I am putting to the test these methods in app and I'll let You know what worked best. Cheers.Vankirk
Hi, I have the same task. Did you find a solution of your problem?Proser
@AlekseyTsyss, not really. I think now that it's impossible to cache EVERYTHING. Maybe some solution appeared in meantime...Vankirk
In case anyone is still looking for a solution, I found a way to store full pages offline using WKWebView https://mcmap.net/q/520561/-swift-ios-cache-wkwebview-content-for-offline-viewRotter
L
7

It sounds like the standard caching is not good enough because you have no control over what will be cached and for how long. The easiest way for solving this is by creating your own caching meganism by overriding the NSURLCache. You can find some documentation about that at http://nshipster.com/nsurlcache/ and a sample at http://github.com/evermeer/EVURLCache That sample even let you use a pre populated cache that can be included in your app install.

Lemire answered 30/5, 2013 at 7:22 Comment(9)
EVURLCache is now updated with a new version of the Reachability class that does support ARCLemire
I have Installed "EVURLCache" through pods. Tested on a blank project which working fine but on an existing project it is not logging anything. Not working I guess. Can you please help me out..??Glottalized
@sairam Thats because WKWebview also does not use NSURLRequest. If you want cashing there then you have to switch to UIWebViewLemire
Yes but UIWebview deprecated in iOS12.0. For me your framework is working <iOS 12.0. It is failing in >iOS 12.0 even with UIWebview. please let me know if we can do something on this to make it work.Singleminded
@Singleminded You are still able to use UIWebView on iOS12. NSURLCache (and therefore also EVURLCache) should still work on iOS 12. It will never work with WKWebview because apple changed the entire technology in there. I haven't seen any alternatives for that.Lemire
@Edwin: I am following all your git repos. I am a good follower for your examples.For me the problem is storeCachedResponse function is not calling at all. Because of this the folders are not creating for the cache. I can see Cache folder under Documents in iOS 11 but it is not available anymore in iOS 12. Can you please help me out what is causing problem here.Singleminded
@Singleminded I think something else is wrong then. If you download the EVURLCache repository and run the demo on iOS 12 and set a breakpoint on storeCachedResponse then you will notice that it's hit. If you still have problems, could you creat an issue in Github so that we have some more space to communicate?Lemire
Downloaded and tested the repo code as well. Method is not calling.Created a ticket ios 12 issue. #62 in github.Singleminded
@EdwinVermeer updated in GitHub with detailed log under #62.Singleminded
S
6
NSString *stringurl=[NSString stringWithFormat:@"http://www.google.com"];
NSURL *url=[NSURL URLWithString:stringurl];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:15.0];
[uiwebview loadRequest:theRequest];

The URL loading system provides a composite on-disk and in-memory cache of responses to requests. This cache allows an application to reduce its dependency on a network connection and increase its performance.

Sardou answered 3/10, 2014 at 6:49 Comment(5)
Note: Currently, only responses to HTTP and HTTPS requests are cached. The FTP and file protocols attempt to access the originating source as allowed by the cache policy. Custom NSURLProtocol classes can optionally provide caching.Sardou
This is what I'm currently doing, but am running into the issue of the website's javascript files not being cached...is it possible to cache js fiels as well as the string url?Galactic
My understanding is that uiwebview caches the url and it's related files.If you want to load a js file offline you have to download it and set the file location.Normaly web view caches the content when using above method.Sardou
Thanks for this. Anyone knows how much data can be cached, can i cache like say 100 urls where each page may have varied size.Runyan
Please read this documentation nshipster.com/nsurlcache. It may help you get a bit more information.Sardou

© 2022 - 2024 — McMap. All rights reserved.