我正在尝试在用户离开允许的网站时显示错误消息。但是,尽管我正在访问允许的网站,但UIAlertController显示“访问被拒绝”。我猜想,允许的网站正在加载一些外部网址,并且我的“ decisionHandler”方法出错。这是代码:
func webView(_ webView: WKWebView,decidePolicyFor navigationaction: WKNavigationaction,decisionHandler: @escaping (WKNavigationactionPolicy) -> Void) {
if let host = navigationaction.request.url?.absoluteString {
if host.contains(selectedWebsite) {
decisionHandler(.allow)
print("? \(host)")
return
}
}
let vc = UIAlertController(title: "access Denied.",message: "Not allowed. Please go Back.",preferredStyle: .alert)
vc.addaction(UIAlertaction(title: "Go Back",style: .default,handler: nil))
present(vc,animated: true)
decisionHandler(.cancel)
print("? \(selectedWebsite)")
}
“ selectedWebsite”是包含允许的URL的字符串数组,存储在viewController中,并被推送到WKWebView。没什么。
例如,当我尝试访问https://stackoverflow.com/时,调试器将显示以下内容:
?https://www.stackoverflow.com/ ?https://www.stackoverflow.com
apple.com也是如此。google.com可以正常工作。
希望你们能帮我解决这个问题。
谢谢!