Simple Webview: navigationDelegate crashes on star

2019-07-31 12:20发布

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.

3条回答
虎瘦雄心在
2楼-- · 2019-07-31 12:50

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.

查看更多
混吃等死
3楼-- · 2019-07-31 12:53

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!

查看更多
Luminary・发光体
4楼-- · 2019-07-31 13:08

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
}
查看更多
登录 后发表回答