1
votes

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.

1
Did you set the web view's navigationDelegate property? - rmaddy
Yes @rmaddy I have used it ``` class BrowserViewController: UIViewController, WKNavigationDelegate ``` - iCodeByte
That does not set the navigationDelegate property. - rmaddy
Can you let me know @rmaddy how it's done?. I'm a beginner so not sure how to set it properly. - iCodeByte
@allanshivji, By BrowserViewController: UIViewController, WKNavigationDelegate, you mean that BrowserViewController is accepting to the WKNavigationDelegate protocol. What you need to do is, In viewDidLoad method, you need to add webView.navigationDelegate = self - Karthick Ramesh

1 Answers

3
votes

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.