How to save UIWebView content for offline display?

懵懂的女人 提交于 2019-12-12 08:13:58

问题


I'm using Xcode 7.2 and Swift to create an iOS app, on this app I display the content of my website, however if I was offline the content will not be shown. So I want to cache the webpage and display for offline.

After I declared everything I'm using the following code :

    var URLPATH="http://google.com"

    let requestURL = NSURL(string: URLPATH)

    let request = NSURLRequest(URL: requestURL!)

    WB.loadRequest(request)

回答1:


  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
  )
}


来源:https://stackoverflow.com/questions/34676149/how-to-save-uiwebview-content-for-offline-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!