Uiwebview Webpage Caching for Offline Viewing

UIWebView webpage caching for offline viewing

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.

Cache a single webpage for use when offline in Xcode / UIWEBVIEW

I never managed to complete the task this way, by did make use of the RNCachingURLProtocol at https://github.com/rnapier/RNCachingURLProtocol/blob/master/RNCachingURLProtocol.h

How to save UIWebView content for offline display?

  1. HTML to Data
if let url = URL(string: urlString) {
person.setValue(try? Data(contentsOf: url), forKey: "content_article")
}

  1. Data to WebView
if let savedObject = fetchedObjects?.first,
let data = savedObject.content_article as? Data,
let baseStringUrl = savedObject.content_url,
let baseURL = URL(string: baseStringUrl) {

webView.load(
data, mimeType: "text/html",
textEncodingName: "",
baseURL: baseURL
)
}

Swift iOS Cache WKWebView content for offline view

I know I'm late, but I have recently been looking for a way to store web pages for offline reading, and still could't find any reliable solution that wouldn't depend on the page itself and wouldn't use the deprecated UIWebView. A lot of people write that one should use the existing HTTP caching, but WebKit seems to do a lot of stuff out-of-process, making it virtually impossible to enforce complete caching (see here or here). However, this question guided me into the right direction. Tinkering with the web archive approach, I found that it's actually quite easy to write your own web archive exporter.

As written in the question, web archives are just plist files, so all it takes is a crawler that extracts the required resources from the HTML page, downloads them all and stores them in a big plist file. This archive file can then later be loaded into the WKWebView via loadFileURL(URL:allowingReadAccessTo:).

I created a demo app that allows archiving from and restoring to a WKWebView using this approach: https://github.com/ernesto-elsaesser/OfflineWebView

EDIT: The archive generation code is now available as standalone Swift package: https://github.com/ernesto-elsaesser/WebArchiver

The implementation only depends on Fuzi for HTML parsing.

Load Offline content in UIWebView

All resources referenced in the HTML file must be cached as well, and the baseURL needs to be a path to those cached resources.

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