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.

Community
  • 1
  • 1
jovanjovanovic
  • 4,158
  • 3
  • 22
  • 32

2 Answers2

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.

Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
  • 1
    EVURLCache is now updated with a new version of the Reachability class that does support ARC – Edwin Vermeer Mar 17 '14 at 09:34
  • 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..?? – onCompletion Dec 16 '16 at 12:48
  • @sairam Thats because WKWebview also does not use NSURLRequest. If you want cashing there then you have to switch to UIWebView – Edwin Vermeer Nov 28 '18 at 14:26
  • Yes but UIWebview deprecated in iOS12.0. For me your framework is working iOS 12.0 even with UIWebview. please let me know if we can do something on this to make it work. – sairam Nov 29 '18 at 04:11
  • @sairam 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. – Edwin Vermeer Nov 29 '18 at 05:57
  • @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. – sairam Nov 29 '18 at 06:38
  • @sairam 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? – Edwin Vermeer Nov 29 '18 at 16:02
  • Downloaded and tested the repo code as well. Method is not calling.Created a ticket **ios 12 issue. #62** in github. – sairam Nov 30 '18 at 09:19
  • @EdwinVermeer updated in GitHub with detailed log under #62. – sairam Dec 04 '18 at 09:33
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.

Rahul K Rajan
  • 776
  • 10
  • 19
  • 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. – Rahul K Rajan Nov 18 '15 at 05:21
  • 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? – narner Apr 26 '16 at 14:25
  • 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. – Rahul K Rajan Apr 27 '16 at 08:43
  • 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. – KD. Nov 17 '17 at 02:11
  • Please read this documentation http://nshipster.com/nsurlcache/. It may help you get a bit more information. – Rahul K Rajan Nov 21 '17 at 11:02