ORStudios ORStudios - 1 year ago 190
iOS Question

How to detect when a UIWebView has completely finished loading?

I am trying to build a filter for a UIWebView and I am struggiling to detect when the UIWebView has completely finished loading. I have used the following two methods

– webView:shouldStartLoadWithRequest:navigationType:
– webViewDidFinishLoad:

but the issue is that these will be called multiple times when a page has frames and additional content to load.

What I need is to know when the view has completely loaded and there is no more content to fetch. Then when the content has loaded I can check to URL of the page against a list of approved URLS.

ANy ideas?

Answer Source

Use the UIWebViewDelegate protocol method webViewDidFinishLoad

Refer to this Apple documentation.

For example... I prepare a custom cell that loads an image into a UIWebView. During the time that it takes to download, I present a UIActivityIndicatorView to show the user the download is progressing. When the UIWebView finishes loading, I override the following example of this delegate method to stop the network activity indicator and stop and remove the UIActivityIndicatorView.

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"%@ - %@", NSStringFromClass(self.class), NSStringFromSelector(_cmd));
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    [<<an instance of UIActivityIndicatorView>> stopAnimating];
    [<<an instance of UIActivityIndicatorView>> removeFromSuperview];