Ethan
Ethan

Reputation: 103

WKNavigationDelegate Crashing App on Assignment

I'm working on a simple web wrapper application for iOS, and I'm having some issues with WKWebView and WKNavigationDelegate. I want to use the didFinishNavigation function from WKNavigationDelegate, so I can grab information from the URL query on navigation (namely a session GUID). My program launches correctly and loads my webpage when I comment out the "webView.navigationDelegate = self" line, but when I include that, my app crashes immediately with the following errors:

 " -[UIWebView setNavigationDelegate:]: unrecognized selector sent to instance 
 0x7f8251e058f0"
"*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[UIWebView setNavigationDelegate:]: unrecognized selector sent to 
instance 0x7f8251e058f0'"

I noticed that both of these error messages include "UIWebView," when I'm trying to use WKWebView, so I tried to use the "custom class" field on the webview from the identity inspector part of the storyboard, but when I try to run after that, I get "(lldb)." Any help/insight would be appreciated, I'm including my code below:

 import UIKit
 import WebKit

class ViewController: UIViewController, WKNavigationDelegate  {

@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()
    print("view did load")

    webView.navigationDelegate = self
    loadURL("http://mydomain/login.html")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

private func loadURL(targetURL: String){
    if let url = NSURL(string: targetURL) {
        let request = NSURLRequest(URL: url)
        webView.loadRequest(request)
    }
}



private func checkDomainGetSessionGUID(){
    print ("we are here")
    if let query = webView.URL?.query {
        let queryArr = query.componentsSeparatedByString("&")
        var parsedQuery : [String: String] = [:]
        for field in queryArr {
            let parts = field.componentsSeparatedByString("=")
            parsedQuery.updateValue(parts[1], forKey: parts[0])
            print ("key =  \(parts[0]) and value = \(parts[1])")
        }
    }
    else {
        print ("didn't enter the if let")
    }
}

func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
    print ("delegate")
   checkDomainGetSessionGUID()
}

}

Upvotes: 5

Views: 4985

Answers (2)

Emre Akcan
Emre Akcan

Reputation: 1160

I was getting this error inside (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation

I removed the line [super webView:webView didFinishNavigation:navigation]; and everything worked as I expected, I'm not sure if it's hacky though.

Upvotes: 1

Elamurugan
Elamurugan

Reputation: 19

PROCEDURE 1:

Step-1: Check UIElements that you are using in Storyboard design. if you had used Web view instead of using **WebKit View this error might come.Check your IBOutlet connection.

Step-2: Check your IOS deployment target, It must be IOS 11 and above because of UIWebKit View was released in IOS 8 but it contains a bug that was fixed in IOS 11 only. So set your deployment target 11 and above.

Step-3: Check your info.plist property. The following property should add in the listApp Transport Security Settings -> Allow Arbitrary Loads -> YES

PROCEDURE 2:

If in case you want deployment target as IOS 10 or below IOS 11 means you can implement like this

Step-1: Create a new UIViewcontroller with its swift ViewController file. And add below-given code to run your URL:

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate {

var webView: WKWebView!

override func loadView() {
    let webConfiguration = WKWebViewConfiguration()
    webView = WKWebView(frame: .zero, configuration: webConfiguration)
    webView.uiDelegate = self
    view = webView
}
override func viewDidLoad() {
    super.viewDidLoad()

    let myURL = URL(string:"https://www.apple.com")
    let myRequest = URLRequest(url: myURL!)
    webView.load(myRequest)
}}

I hope this might be helpful to you...

Upvotes: 2

Related Questions