Search code examples
iosswiftwkwebview

_webView didFinish Navigation is not Working


I'm trying to enable and disable network activity indicator but after the page has been loaded it does not call didFinish Navigation function.

I'm using WKWebView. First I have loaded the url using webView.load(). Then I have called didFinish Navigation function but it not reaching that function. I have tried all other posts but it just does not work. In info.plist file I have added App Transport Security Setting and inside it I have added Allow Arbitrary Loads, NSAllowsArbitraryLoadsForMedia, Allow Arbitrary Loads in Web Content and have set all of its value to Boolean and YES.

override func viewDidLoad() {
        super.viewDidLoad()

        if !CheckInternet.Connection() {
            checkInternetConnection()
        }

        UIApplication.shared.isNetworkActivityIndicatorVisible = true
        webView.load(URLRequest(url: URL(string: "https://www.google.com")!))
    }

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("Loaded...")
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
    }

Above code does not print "Loaded...". My network activity indicator turns ON but I need to stop it after the webpage has loaded successfully.


Solution

  • In viewDidLoad you need to set the delegate of WEWebView.

    webView.navigationDelegate = self
    

    You can also set the delegate in .xib/ storyboard by navigating to the connections inspector of WKWebview and then drag-dropping the delegate to the BrowserViewController.