How to check if WkWebView finish loading in Objective-C?

faklyasgy picture faklyasgy · Mar 30, 2016 · Viewed 41.3k times · Source

I want to load HTML pages using WkWebView and I want to show the page just after it's finished loading. As long as it's loading I would like to show an activity indicator on an empty View. I create two view a loadingView and a wkWebView. While the page is loading I add to VC as subview the loadingView and after I want to remove loadingView and add wkWebView. Here is my code:

    [self addSubview:_loadingView];
    _wkWebView = [[WKWebView alloc] initWithFrame:self.frame];
    _wkWebView.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);

    //Send a request to wkUrlconnection
    NSURL *wkUrl = [NSURL URLWithString:self.wkUrlString];
    NSURLRequest *wkRequest = [NSURLRequest requestWithURL:wkUrl];

    //Here I want to check if it's loaded and then remove loadingView and add wkWebView
    [_wkWebView loadRequest:wkRequest];
    [self.loadingView removeFromSuperview];
    [self addSubview:_wkWebView];

Can someone show me how to check while it's loading and if finish refresh the VC? Thank you for your answers.

Answer

Stavash picture Stavash · Mar 30, 2016

I think the WKNavigationDelegate's webView:didFinishNavigation: delegate callback is what you're looking for.

Configure and present your activity indicator when you start to load and then stop and remove it from view when the callback is called.