我正在使用 UIWebView 和以下代码将网页加载到我的应用程序中:
let url = NSURL (string: "http://www.myurl.com");
let requestObj = NSURLRequest(URL: url!);
myWebView.loadRequest(requestObj);
如果请求因任何原因失败,例如因为无法访问互联网,我想隐藏 webView。有办法吗?
最佳答案
使用UIWebView
提供的委托(delegate)方法
func webViewDidFinishLoad(webView: UIWebView) {
print("webview did finish load!")
}
func webView(webView: UIWebView, didFailLoadWithError error: NSError) {
print("webview did fail load with error: \(error)")
}
注意:要使用此方法,您需要在 ViewController
中实现 UIWebViewDelegate
并使用 webView outlet 设置它。
myWebView.delegate = self
关于ios - Swift 中的 UIWebView 离线错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38661451/