从 UIWebView 到 WKWebView 的迁移

我正在从 UIWebView 迁移到 WKWebView,如何为 WKWebView 重写这些函数?

func webViewDidStartLoad(webView: UIWebView){}
func webViewDidFinishLoad(webView: UIWebView){}

还有

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
print("webview asking for permission to start loading")
if navigationType == .LinkActivated && !(request.URL?.absoluteString.hasPrefix("http://www.myWebSite.com/exemlpe"))!{
UIApplication.sharedApplication().openURL(request.URL!)
print(request.URL?.absoluteString)
return false
}
print(request.URL?.absoluteString)
lastUrl = (request.URL?.absoluteString)!


return true
}




func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
print("webview did fail load with error: \(error)")
let testHTML = NSBundle.mainBundle().pathForResource("back-error-bottom", ofType: "jpg")
let baseUrl = NSURL(fileURLWithPath: testHTML!)
        

let htmlString:String! = "myErrorinHTML"
self.webView.loadHTMLString(htmlString, baseURL: baseUrl)
}
73900 次浏览

UIWebView = > WKWebView 等效

UIWebViewDelegate => WKNavigationDelegate


delegate => navigationDelegate
didFailLoadWithError => didFailNavigation
webViewDidFinishLoad => didFinishNavigation
webViewDidStartLoad => didStartProvisionalNavigation
shouldStartLoadWithRequest => decidePolicyForNavigationAction

关于 shouldStartLoadWithRequest你可以写:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
print("webView:\(webView) decidePolicyForNavigationAction:\(navigationAction) decisionHandler:\(decisionHandler)")
    

switch navigationAction.navigationType {
case .linkActivated:
if navigationAction.targetFrame == nil {
self.webView?.loadRequest(navigationAction.request)
}
if let url = navigationAction.request.url, !url.absoluteString.hasPrefix("http://www.myWebSite.com/example") {
UIApplication.shared.open(url)
print(url.absoluteString)
decisionHandler(.cancel)
return
}
default:
break
}
    

if let url = navigationAction.request.url {
print(url.absoluteString)
}
decisionHandler(.allow)
}

至于 didFailLoadWithError:

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print("webView:\(webView) didFailNavigation:\(navigation) withError:\(error)")
let testHTML = Bundle.main.path(forResource: "back-error-bottom", ofType: "jpg")
let baseUrl = URL(fileURLWithPath: testHTML!)


let htmlString = "myErrorInHTML"
self.webView.loadHTMLString(htmlString, baseURL: baseUrl)
}

将 UIWebView 迁移到 WKWebView,Swift 4:

相当于 shouldStartLoadWithRequest:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
var action: WKNavigationActionPolicy?


defer {
decisionHandler(action ?? .allow)
}


guard let url = navigationAction.request.url else { return }


print(url)


if navigationAction.navigationType == .linkActivated, url.absoluteString.hasPrefix("http://www.example.com/open-in-safari") {
action = .cancel                  // Stop in WebView
UIApplication.shared.openURL(url) // Open in Safari
}
}

相当于 webViewDidStartLoad:

func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print(String(describing: webView.url))
}

相当于 didFailLoadWithError:

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
let nserror = error as NSError
if nserror.code != NSURLErrorCancelled {
webView.loadHTMLString("404 - Page Not Found", baseURL: URL(string: "http://www.example.com/"))
}
}

相当于 webViewDidFinishLoad:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print(String(describing: webView.url))
}

下面是 迁徙目标 C方法:

  1. Should dStartLoadWithRequest-> decidePolicyForNavigationAction

记得打电话给 decisionHandler:

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {


}
NSString *url = [navigationAction.request.URL query];
                

decisionHandler(WKNavigationActionPolicyAllow);
}
  1. WebViewDidStartLoad-> did StartProvisional導航
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
}
  1. WebViewDidFinishLoad-> didiFinishNavigationwebViewDidFinishLoad-> didiFinishNavigationwebViewDidFinishLoad
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
}
  1. = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
}