Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save dynamic webpage in cache using UIWebview in Swift 3

I am developing an application(Swift 3 using UIWebview). I need to load webpages into webview and save some webpages into cache. If there is not internet user will able to see those pages. But I am confused on how to save whole webpage in cache. The main thing is we need to show pages back even if there is not internet.

I used the following documention : http://nshipster.com/nsurlcache/ and https://developer.apple.com/reference/foundation/urlcache

let url = NSURL(string: load_url1)
let request = NSURLRequest(url: url as! URL,cachePolicy: NSURLRequest.CachePolicy.returnCacheDataElseLoad, timeoutInterval: 60)
self.webView.loadRequest(request as URLRequest);

Has anyone implemented this before. Please provide some demo code as this is my first attempt on cache

like image 699
S.Sathya Priya Avatar asked Feb 07 '17 13:02

S.Sathya Priya


1 Answers

Just use

let url = URL(string: urlString)
var urlRequest = URLRequest(url: url!)
urlRequest.cachePolicy = .returnCacheDataElseLoad
webView.loadRequest(urlRequest)
like image 90
LNI Avatar answered Oct 07 '22 17:10

LNI