14

I have to migrate from UIWebView to WKWebView and I'm having some trouble with this. It looks like that "WKWebView" has no member 'request'. Is there a way to handle this situation using the WKWebView? I simply need to get the URL loaded and then if it contains ".pdf" just show a button.

func webViewDidFinishLoad(_ webView : WKWebView) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = false

    if (webView.request?.url!.absoluteString)!.range(of: ".pdf") != nil {
        pdfBackButton.isHidden = false
    }
    else {
    pdfBackButton.isHidden = true}

}
toddg
  • 2,863
  • 2
  • 18
  • 33
ernestocattaneo
  • 1,197
  • 5
  • 18
  • 30
  • Try `webView.url` More info here: http://stackoverflow.com/questions/26342546/wkwebview-full-url – toddg Nov 22 '16 at 16:17
  • @toddg unfortunately it is not enough and if I want to print the current URL by doing print(webView.url) it does'nt work... – ernestocattaneo Nov 23 '16 at 08:07

2 Answers2

11

Add "WebKit" framework to your class.

override func viewDidLoad() {
     super.viewDidLoad()
     let myWebView:WKWebView = WKWebView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
     myWebView.navigationDelegate = self;
     let url = URL(string: "https://www.Apple.com")!
     myWebView.load(URLRequest(url: url))
     self.view.addSubview(myWebView)
}

then implement WKNavigationDelegate method

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = false
    let url = webView.url
    print(url as Any) // this will print url address as option field
    if url?.absoluteString.range(of: ".pdf") != nil {
         pdfBackButton.isHidden = false
         print("PDF contain")
    }
    else {
         pdfBackButton.isHidden = true
         print("No PDF Contain")        
    } 
}

Hope this will help you!

  • If I put this line "myWebView.navigationDelegate = self;" the application instantly crashes when starting. I've read that it is not possible anymore to use this with the WKWebView.... could be this the problem? If I use your code without that line I can run it but I then have the same problem: the function didFinish looks like it is never called. Infact I don't see the URLs loaded.... – ernestocattaneo Nov 23 '16 at 14:33
  • I have tried this and it is working fine..! looks like you are missing out something.. – Mangesh Kondaskar Nov 28 '16 at 07:04
  • send me crash report or log so that i can analyse it help you to resolve the issue...!! – Mangesh Kondaskar Nov 28 '16 at 10:29
  • @ernestocattaneo I was having the same problem, but figured it out. I had inserted the UIView using a storyboard and forgot to delete it and reinsert the WKWebView instead. Once I did that the above code worked like a champ. – Scooter Jun 30 '20 at 17:02
6

You can implement the WKNavigationDelegate webView(_:didFinish:) function.

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = false
    let url = webView.url

    if url?.absoluteString.range(of: ".pdf") != nil {
        pdfBackButton.isHidden = false
    }
    else {
        pdfBackButton.isHidden = true
    }
}
mservidio
  • 12,817
  • 9
  • 58
  • 84
  • thanks for your help but unfortunately it doesn't work... I added a simple "print(url!)" after the declaration of the url but the current url loaded is never printed in the console and so I guess that this method is never called... any idea on that? – ernestocattaneo Nov 23 '16 at 08:11
  • Did you set the WKWebView delegate? – toddg Nov 23 '16 at 14:24
  • @toddg I'm actually having problem on this I guess. When I set "webView.navigationDelegate = self" in the "viewDidLoad" method, then my application crashes on startup... – ernestocattaneo Nov 24 '16 at 08:28
  • http://stackoverflow.com/questions/40784033/simple-webview-navigationdelegate-crashes-on-start I've created this other question regarding the delegate :) – ernestocattaneo Nov 24 '16 at 10:35