如何使用Swift 3中的UIWebview在缓存中保存动态网页

时间:2022-03-02 03:52:11

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.

我正在开发一个应用程序(使用UIWebview的Swift 3)。我需要将网页加载到webview中并将一些网页保存到缓存中。如果没有互联网用户将能够看到这些页面。但我对如何在缓存中保存整个网页感到困惑。主要的是我们需要显示页面,即使没有互联网。

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

我使用了以下文档:http://nshipster.com/nsurlcache/和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

有没有人以前实现过这个。请提供一些演示代码,因为这是我第一次尝试缓存

1 个解决方案

#1


2  

Just use

只是用

let url = URL(string: urlString)
var urlRequest = URLRequest(url: url!)
urlRequest.cachePolicy = .returnCacheDataElseLoad
webView.loadRequest(urlRequest)

#1


2  

Just use

只是用

let url = URL(string: urlString)
var urlRequest = URLRequest(url: url!)
urlRequest.cachePolicy = .returnCacheDataElseLoad
webView.loadRequest(urlRequest)