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
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.
You don't initialize the
webView
. The app crashes because it'snil
.You need to bind it to the storyboard or the xib file (uncomment the
@IBOutlet
) or initialize it programmatically and then add it to youviewController's
view
.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.
then implement WKNavigationDelegate method
Hope this will help you!
You are binding an
UIWebView
to a property of typeWKWebView
. As a result it crashes when trying to callnavigationDelegate
on theUIWebView
, 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: