dimaskpz
dimaskpz

Reputation: 87

Cannot assign value of type 'ViewController' to type 'WKNavigationDelegate?'

I'm new to use webkit.

I want to give delegate my WKWebView to my view controller, but it give me error:

Cannot assign value of type 'ViewController' to type 'WKNavigationDelegate?'

My code:

import UIKit
import WebKit
class ViewController: UIViewController {

    @IBOutlet weak var webKitComponent: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        webKitComponent.navigationDelegate = self
    }

}

Upvotes: 0

Views: 3349

Answers (2)

Gerald Eersteling
Gerald Eersteling

Reputation: 1284

To add to @Sh_Khan's answer; the reason for your error is that the class ViewController doesn't conform to the protocol WKNavigationDelegate out of the box.

You need to make it conform by adding the protocol and it's required methods, as Sh_Khan showed you.

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100503

Add

extension ViewController: WKNavigationDelegate {
    private func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
        debugPrint("didCommit")
    }

    private func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        debugPrint("didFinish")
    }

    private func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        debugPrint("didFail")
    }
}

Upvotes: 1

Related Questions