enter image description here我想将这个webview网页保存在文档目录中,我这样做了:
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let text = UserDefaults.standard.string(forKey: "path")
let dataPath = documentsDirectory.appendingPathComponent("\(text!)/\(pageTitle).html")
print("path is:\(dataPath)")
do
{
let url: NSURL = NSURL(string: textfield_url.text!)!
let urlData: NSData = try NSData(contentsOf: url as URL)
urlData.write(toFile: dataPath.path, atomically: true)
} catch let error as NSError {
print("Ooops! Something went wrong: \(error)")
}
textfield包含当前页面网址。 现在我想从文档目录加载这个网页,问题是我的页面没有加载完全丢失的图像。 对于加载代码是:
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let dataPath = documentsDirectory.appendingPathComponent(pageName)
print("path is:\(dataPath)")
let url = URL.init(fileURLWithPath: dataPath.path);
let requestObj = URLRequest.init(url: url)
webView.loadRequest(requestObj)
加载后我得到这个: enter image description here