WebKit - webView.navigationDelegate = self

Hello everybody,


I am new to iOS development. I am trying to to load a web view into my app. I got it working but there is one line of code that I fundamentally don't understand.


override func loadView() {
        webView = WKWebView()
        webView.navigationDelegate = self
        view = webView //Cada ViewController tem uma view associada.
    }


What am I telling to the view when I write webView.navigationDelegate = self? In my head it makes much more sense if it was:


self.view = webView.navigationDelegate   


Can you help me?


Thank you! 😀

Answered by Claude31 in 406547022

You probably need to learn about delegate.


override func loadView() { 
        webView = WKWebView() 
        webView.navigationDelegate = self 
        view = webView //Cada ViewController tem uma view associada. 
    }


What line 3 means:

- I have a webView created line 2

- to work, the webView needs to rely on someone (a controller) that implements some functions (here for navigating), by delegation, needed to make it work.

- It thus have to tell compiler who it is going to delegate this role to execute the functions

- webView.navigationDelegate = self means that webView navigationDelegate is 'self', that is the class in which webView is declared.


Now, in the class, you will see the delegate functions associated to

WKNavigationDelegate protocol (see the doc):


WKNavigationDelegate


The methods of the

WKNavigationDelegate
protocol help you implement custom behaviors that are triggered during a web view's process of accepting, loading, and completing a navigation request.



Is that clear now (don't worry if you have some difficulty to grab completely the delegation concept: it is a bit hard at the beginning).

Don't forget to close the thread if OK.


Accepted Answer

You probably need to learn about delegate.


override func loadView() { 
        webView = WKWebView() 
        webView.navigationDelegate = self 
        view = webView //Cada ViewController tem uma view associada. 
    }


What line 3 means:

- I have a webView created line 2

- to work, the webView needs to rely on someone (a controller) that implements some functions (here for navigating), by delegation, needed to make it work.

- It thus have to tell compiler who it is going to delegate this role to execute the functions

- webView.navigationDelegate = self means that webView navigationDelegate is 'self', that is the class in which webView is declared.


Now, in the class, you will see the delegate functions associated to

WKNavigationDelegate protocol (see the doc):


WKNavigationDelegate


The methods of the

WKNavigationDelegate
protocol help you implement custom behaviors that are triggered during a web view's process of accepting, loading, and completing a navigation request.



Is that clear now (don't worry if you have some difficulty to grab completely the delegation concept: it is a bit hard at the beginning).

Don't forget to close the thread if OK.


So basically what I am saying is that I am going to trust that the classe that implements the WebView itself has all the functions that are needed?

Yes.


So basically what I am saying is that I am going to trust that the class that implements the WebView itself has all the functions that are needed?

The class is typically a UIViewController in which you have override func loadView().


In the definition of a protocol, some functions are 'required', some other 'optional'.

If it is required (see the doc), you have to implement it in the class (that is what you do for instance with tableViews).

Otherwise, it is up to depending if you need to do something special when this func is called by the system.

WebKit - webView.navigationDelegate = self
 
 
Q