ernestocattaneo
ernestocattaneo

Reputation: 1207

Simple Webview: navigationDelegate crashes on start

I have a small problem with my WebView. This is what I do in my app:

import UIKit
import WebKit

class ViewController: UIViewController,  WKNavigationDelegate  {

@IBOutlet var webView: WKWebView! // draged from storyboard

override func viewDidLoad() {
    super.viewDidLoad()

    var urlToLoad : (String)
    urlToLoad = "http://wwww.myapp.com"

    webView.sizeToFit()
    webView.isUserInteractionEnabled = true

    let url = URL(string: urlToLoad)
    let request = URLRequest(url:url!)
    //webView.navigationDelegate = self
    webView.load(request)
}

As you see the row "webView.navigationDelegate = self" is commented. If I uncomment it my app will crash on start and telling me this:

Debugger: libc++abi.dylib: terminating with uncaught exception of type NSException

enter image description here

Any idea of why is this happening and how to solve this? I need this in order to use methods like "didFinish navigation" that I already implemented in the page but they are never called.

Also, by having a look at my code... you see anything I should change regarding the use of my webview?

Thank you for helping me.

Upvotes: 1

Views: 6038

Answers (4)

kmanzana
kmanzana

Reputation: 1198

I was getting the error Value of type '(WKWebView, WKNavigation?) -> ()' has no member 'navigationDelegate' (which I'm guessing was the same issue as what was causing the crash when adding webView.navigationDelegate = self).

I found that the view controller class needs to implement the WKNavigationDelegate protocol (kinda like inheritance) by adding it to the class definition, which fixed the error for me. Make sure to have that as in the first line of this code:

class ViewController: UIViewController, WKNavigationDelegate {
  // ...

  override func loadView() {
    webView = WKWebView()
    webView.navigationDelegate = self
  }

  func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
     // ...
  }      
}

Upvotes: 0

Mangesh Kondaskar
Mangesh Kondaskar

Reputation: 214

You are using UIWebView and trying to call the methods of WKWebView which is leading to crash your app due to unrecognised selector send on UIWebView.

Try to create new project and use below mentioned code.

import "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!

Upvotes: 2

shallowThought
shallowThought

Reputation: 19602

You are binding an UIWebView to a property of type WKWebView. As a result it crashes when trying to call navigationDelegate on the UIWebView, as it does not offer this method.

There is no template version of WKWebView available in Storyboard, so you have to create the WKWebView programatically:

import UIKit
import WebKit

class ViewController: UIViewController,  WKNavigationDelegate  {

var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    webView = WKWebView(frame: view.frame)
    webView.navigationDelegate = self
    view.addSubview(webView)

    let url = URL(string: "https://www.google.com")!
    webView.load(URLRequest(url: url))
    webView.allowsBackForwardNavigationGestures = true
}

Upvotes: 1

Max Pevsner
Max Pevsner

Reputation: 4104

You don't initialize the webView. The app crashes because it's nil.

You need to bind it to the storyboard or the xib file (uncomment the @IBOutlet) or initialize it programmatically and then add it to you viewController's view.

Upvotes: 0

Related Questions