UIWebView页面加载出错,我们应该怎么解决呢?
在使用webview时,会存在这么一个问题,webview网页加载出现错误!
如图:
这样就造成界面不美观,那么如何解决这个问题???
解决方案:
在 viewDidLoad 中
let request = URLRequest(url: URL(string: urlString)!)
let data = URLSession.shared.dataTask(with: request) { (data, response, error) in
let tmp = response as? HTTPURLResponse
if tmp?.statusCode != 200 {
let path = Bundle.main.path(forResource: "error", ofType: "html")!
let url = URL(fileURLWithPath: path)
self.myWebView.loadRequest(URLRequest(url: url))
return
}else {
self.myWebView.loadRequest(URLRequest(url: URL(string: self.urlString)!))
}
}
data.resume()
网友评论