在没有网络时在网页中执行操作时关闭 WKWebview
Dismiss WKWebview when there is no network on performing an action in a web page
我正在 WKWebView
中加载注册表单,当用户填写注册表单并点击“注册”按钮时,它将转到“注册成功”页面。
如果用户在点击“注册”按钮时失去网络连接,如何捕获无网络错误?
如果在加载注册表单之前用户未连接到网络,则以下代码有效:
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
if(error.localizedDescription == "The Internet connection appears to be offline.") {
print("Network error")
self.presentingViewController?.dismiss(animated: true, completion: {
let alert = UIAlertController(title: "No Network", message: "No network, please check your internet connection and try again.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK".localisedString(), style: .default, handler: nil))
let topC = AppConstants.getRootController().topMostViewController()
topC.present(alert, animated: true, completion: nil)
})
}
}
- 您首先关闭了当前的
ViewController
,然后您正在尝试穿鞋 UIAlertController
,也许这就是它不起作用的原因,否则您的代码是正确的
尝试先显示 alert
然后关闭当前 ViewController
如下所示
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
if(error.localizedDescription == "The Internet connection appears to be offline.")
{
print("Network error")
let alert = UIAlertController(title: "No Network", message: "No network, please check your internet connection and try again.", preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default) { ( _ ) in
let topC = self.storyboard!.instantiateViewController(withIdentifier: "cal") as! cal
self.present(topC, animated: true, completion: nil)
}
alert.addAction(ok)
self.present(alert, animated: true) {
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
}
}
试试这个代码,这会起作用(代码中缺少的内容如下)
ok
AlertAction 未添加到 alert
AlertViewController
topC
已初始化,但未在 ok
操作中显示。
我正在 WKWebView
中加载注册表单,当用户填写注册表单并点击“注册”按钮时,它将转到“注册成功”页面。
如果用户在点击“注册”按钮时失去网络连接,如何捕获无网络错误?
如果在加载注册表单之前用户未连接到网络,则以下代码有效:
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
if(error.localizedDescription == "The Internet connection appears to be offline.") {
print("Network error")
self.presentingViewController?.dismiss(animated: true, completion: {
let alert = UIAlertController(title: "No Network", message: "No network, please check your internet connection and try again.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK".localisedString(), style: .default, handler: nil))
let topC = AppConstants.getRootController().topMostViewController()
topC.present(alert, animated: true, completion: nil)
})
}
}
- 您首先关闭了当前的
ViewController
,然后您正在尝试穿鞋UIAlertController
,也许这就是它不起作用的原因,否则您的代码是正确的 尝试先显示
alert
然后关闭当前ViewController
如下所示func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { if(error.localizedDescription == "The Internet connection appears to be offline.") { print("Network error") let alert = UIAlertController(title: "No Network", message: "No network, please check your internet connection and try again.", preferredStyle: .alert) let ok = UIAlertAction(title: "OK", style: .default) { ( _ ) in let topC = self.storyboard!.instantiateViewController(withIdentifier: "cal") as! cal self.present(topC, animated: true, completion: nil) } alert.addAction(ok) self.present(alert, animated: true) { self.presentingViewController?.dismiss(animated: true, completion: nil) } } }
试试这个代码,这会起作用(代码中缺少的内容如下)
ok
AlertAction 未添加到alert
AlertViewControllertopC
已初始化,但未在ok
操作中显示。