6

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

S.Sathya Priya
  • 470
  • 1
  • 5
  • 19

1 Answers1

8

Just use

let url = URL(string: urlString)
var urlRequest = URLRequest(url: url!)
urlRequest.cachePolicy = .returnCacheDataElseLoad
webView.loadRequest(urlRequest)
LNI
  • 2,935
  • 2
  • 21
  • 25