turbolinks / turbolinks-ios

Native iOS adapter for building hybrid apps with Turbolinks 5
MIT License
881 stars 92 forks source link

custom WKNavigationDelegate doesn't work #56

Closed jun1st closed 8 years ago

jun1st commented 8 years ago

I'd like get some data from response headers, I think it's inside WKNavigation object. But my custom delegate not get called.

I've set my view controller as the delegate

func sessionDidLoadWebView(session: Session) {
        session.webView.navigationDelegate = self
}

here is the summary of my delegate

func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
    if let _ = navigationAction.request.URL?.absoluteString.rangeOfString("itunes.apple.com") {
        UIApplication.sharedApplication().openURL(navigationAction.request.URL!)
        decisionHandler(WKNavigationActionPolicy.Cancel)
        return
    }
    decisionHandler(WKNavigationActionPolicy.Allow)
}

func webView(webView: WKWebView, didCommitNavigation navigation: WKNavigation!) {
    print("loading...")
}

func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
    print("cool...")
}

the decidePolicyForNavigationAction method did get called, but not the other two,

Did I miss anything?

zachwaugh commented 8 years ago

The webView:didCommitNavigation and webView:didFinishNavigation would only get called on the initial "cold boot" request, which by the time sessionDidLoadWebView is called, would have already completed. All other requests are handled by Turbolinks in JavaScript using xhr, so they don't hit the WKNavigationDelegate load methods.

Your best bet is to add a JavaScript event handler to listen to turbolinks:request-end to access the response headers - https://github.com/turbolinks/turbolinks#full-list-of-events.