从UIWebView迁移到WKWebView

在我的应用程序,我正在从UIWebView迁移到WKWebView,我怎样才能重写WKWebView的这些function?

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) } 

UIWebView => WKWebView等效

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

关于shouldStartLoadWithRequest你可以写:

 func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((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 where !url.absoluteString.hasPrefix("http://www.myWebSite.com/example") { UIApplication.sharedApplication().openURL(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, didFailNavigation navigation: WKNavigation, withError error: NSError) { print("webView:\(webView) didFailNavigation:\(navigation) withError:\(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) }