Swift iOS Cache WKWebView content for offline view

前端 未结 4 825
礼貌的吻别
礼貌的吻别 2020-12-08 03:34

We\'re trying to save the content (HTML) of WKWebView in a persistent storage (NSUserDefaults, CoreData or disk file). The user can see the same content when he re-enters th

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 04:09

    Easiest way to use cache webpage is as following in Swift 4.0: -

    /* Where isCacheLoad = true (Offline load data) & isCacheLoad = false (Normal load data) */

    internal func loadWebPage(fromCache isCacheLoad: Bool = false) {
    
        guard let url =  url else { return }
        let request = URLRequest(url: url, cachePolicy: (isCacheLoad ? .returnCacheDataElseLoad: .reloadRevalidatingCacheData), timeoutInterval: 50)
            //URLRequest(url: url)
        DispatchQueue.main.async { [weak self] in
            self?.webView.load(request)
        }
    }
    

提交回复
热议问题