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! 😀
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.