我创建了一个名为JSBridge 的类,这个类实现了WKNavigationDelegate ,我在protocol方法中做了一些自定义的东西,然后调用另一个UIViewController 的默认WKNavigationDelegate ,例如WebViewController 又名webViewDelegate ,所以我必须这样做
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let webViewDelegate = webViewDelegate , self.webView == webView else {
decisionHandler(.allow)
return
}
//do something here then call `WebViewController`'s implementation
if webViewDelegate.responds(to: #selector(webViewDelegate.webView(_:decidePolicyFor:decisionHandler)) {
webViewDelegate.webView!(webView, decidePolicyFor: navigationAction, decisionHandler: decisionHandler)
} else {
decisionHandler(.allow)
}
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
guard let webViewDelegate = webViewDelegate , self.webView == webView else {
decisionHandler(.allow)
return
}
//do something here then
if webViewDelegate.responds(to: #selector(webViewDelegate.webView(_:decidePolicyFor:decisionHandler)) {
webViewDelegate.webView!(webView, decidePolicyFor: navigationResponse, decisionHandler: decisionHandler)
} else {
decisionHandler(.allow)
}
}
我的问题出在 xcode8(swift3) 中,编译器给了我错误:
'webView(_:decidePolicyFor:decisionHandler'的歧义使用 ,它们与编译器同名
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void)
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
Best Answer-推荐答案 strong>
您可以使用 as 从一些模棱两可的方法中选择一种:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let webViewDelegate = webViewDelegate , self.webView == webView else {
decisionHandler(.allow)
return
}
typealias WKNavigationActionMethodType = (WKWebView,WKNavigationAction,@escaping(WKNavigationActionPolicy)->Void) -> Void
if webViewDelegate.responds(to: #selector(webView(_:decidePolicyFor:decisionHandler as WKNavigationActionMethodType)) {
webViewDelegate.webView!(webView, decidePolicyFor: navigationAction, decisionHandler: decisionHandler)
} else {
decisionHandler(.allow)
}
}
另一种处理这种情况的方法是“避免使用 responds(to 并使用可选链接”:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let webViewDelegate = webViewDelegate , self.webView == webView else {
decisionHandler(.allow)
return
}
if webViewDelegate.webView?(webView, decidePolicyFor: navigationAction, decisionHandler: decisionHandler) == nil {
decisionHandler(.allow)
}
}
委托(delegate)方法的返回类型是Void ,所以它的Optional链结结果是Void? 类型。在您的情况下,只有在实例中未实现可选方法时,它才会变为 nil 。因此,将结果与 nil 进行比较是在检查可选方法是否已实现(并实际调用)。
关于ios - swift 3 WKWebView 委托(delegate) WKNavigationDelegate 模糊方法,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/39658358/
|