Migration von UIWebView zu WKWebView

106

In meiner App migriere ich von UIWebView zu WKWebView. Wie kann ich diese Funktion für WKWebView umschreiben?

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

und

    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)
    }
Phocs
quelle
Klingt wie Sie sollten einen Blick auf das nehmen navigationDelegatevon der WKWebView.
luk2302
Ähnliches Problem - kann Ihnen helfen - stackoverflow.com/questions/46793618/ios-wkwebview-vs-uiwebview
Krunal

Antworten:

204

UIWebView => WKWebView-Äquivalent

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

Über shouldStartLoadWithRequestSie können schreiben:

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

Und für die 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)
}
Alessandro Ornano
quelle
1
webViewDidFinishLoad und didFinishNavigation sind nicht genau gleich. Die didFinishNavigation-Methode wird aufgerufen, bevor die Seite vollständig geladen ist.
Michal Cichon
Die Dokumentation besagt, dass diese Methode nur für OSX gilt. Die Frage war für iOS
Gargo
1
@Gargo Schauen Sie sich beide Beschreibungen an: webViewDidFinishLoad , didFinishNavigation , wie Sie sehen, ist auch für iOS 8.0+ verfügbar
Alessandro Ornano
stackoverflow.com/questions/46171410/… - jemand hilft mir bei diesem Problem @AlessandroOrnano
Siva
4
Beachten Sie nur kurz, dass diese Methoden in WKNavigationDelegate(nicht WKUIDelegate) deklariert sind .
Mike Keskinov
30

Hier sind die Objective-C- Methoden für die Migration

1) shouldStartLoadWithRequest -> EntscheidungPolicyForNavigationAction

Denken Sie daran, die anzurufen decisionHandler

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

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

       decisionHandler(WKNavigationActionPolicyAllow);
}

2) webViewDidStartLoad -> didStartProvisionalNavigation

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
}

3) webViewDidFinishLoad -> didFinishNavigation

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
}

4) didFailLoadWithError -> didFailNavigation

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
}
Akila Wasala
quelle
3
sollte das WKNavigationTypeLinkActivatedeher sein als UIWebViewNavigationTypeLinkClicked?
CupawnTae
12

Migrieren von UIWebView zu WKWebView, Swift 4 :

Äquivalent zu 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
    }
}

Äquivalent zu webViewDidStartLoad:

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

Äquivalent zu 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/"))
    }
}

Äquivalent zu webViewDidFinishLoad:

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