从 UIWebView 迁移到 WKWebView [英] Migrating from UIWebView to WKWebView

查看:18
本文介绍了从 UIWebView 迁移到 WKWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我正在从 UIWebView 迁移到 WKWebView ,如何为 WKWebView 重写这些函数?

in my app I'm migrating from UIWebView to WKWebView , how can I rewrite these function for 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)
    }

推荐答案

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆