1
votes

I have an IOS app that navigates to a web page using a WKWebView. The page itself contains back buttons in the menu. When we click on a link to a PDF, the pdf itself displays in the view, but the other html content (including back buttons) disappears.

Is there any way for me to detect when a link returns a PDF and open that content externally in safari or a similar browser?

If that's not possible is there a way for me to detect when a link returns a PDF and show the web kit back buttons (just for that page).

Edit: Clarification

The link clicked to go to the pdf doesn't actually contain the pdf text. The link looks something like /Documents/GetDocument?docid=1234.

The content returned is of type application/pdf. In an ideal world we would be able to see the content returned from the link, and then handle appropriately.

1

1 Answers

2
votes

You can do something like this

  extension ViewController: WKNavigationDelegate{
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        print(navigationAction.request.url ?? "")
        if let url = navigationAction.request.url?.absoluteString{

        }
        decisionHandler(.allow)
    }
}

And don't forget to assign

 webView.navigationDelegate = self

The above method will get called when ever webview tries to load a new url and as per your comment and updated question you want to check the mime type of url. For that you can refer to

  1. This SO Answer

  2. Library