Detect all UIWebViews load finish : Objective-C

798 Views Asked by At

As mentioned in the title, I'm looking out for the way to detect all of the UIWebViews has finished load that are loading HTML string simultaneously.

I have one custom view in which there are 5 web views (1 for question and 4 for options). Each of them loading html string as :

[queWV loadHTMLString:queHtml baseURL:nil];
[opt1WV loadHTMLString:opt1Html baseURL:nil];
[opt2WV loadHTMLString:opt2Html baseURL:nil];
[opt3WV loadHTMLString:opt3Html baseURL:nil];
[opt4WV loadHTMLString:opt4Html baseURL:nil];

I've tried checking this with webViewDidFinishLoad

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
     if(webView == opt4WV)
     {
         //All web views has finished loading
     }    
}

But practically, its incorrect as the webViewDidFinishLoad method is getting called for random web views, depending on contents to load.

Is there any way to detect load finish for all the web views ?

Thanks !

5

There are 5 best solutions below

1
On

Maybe I'm oversimplifying or not seeing something, but did you try to use webViewDidFinishLoad in a combination with the counter? So if you have 5 web views, implement a counter from 1 to 5 and count how many times does webViewDidFinishLoad get's called. When you get to 5 all views are loaded.

0
On

yes, use a counter to keep track and make sure all web views delegate are set to the same controller

0
On

declare the variable, "loaded" out of this function

int loaded = 0

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
     if(webView == queWV || webView == opt1WV || webView == opt2WV || webView == opt3WV || webView == opt4WV )
     {
         loaded += 1;
     }    
    
      if (loaded ==5) {
        // process
      }
}

0
On

in .h file create one counter like this

int webViewLoadCount = 0;

then trke this counter like this way

in .m file UIWebview delegate method

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *string = webView.request.URL.absoluteString;

    if ([string rangeOfString:queHtml]) {
        NSLog(@"queWV webview loaded with this  %@ ",queHtml);
        webViewLoadCount = webViewLoadCount + 1;
    } else if ([string rangeOfString:opt1Html]) {
        NSLog(@"opt1WV webview loaded with this  %@ ",opt1Html);
        webViewLoadCount = webViewLoadCount + 1;
    } else if ([string rangeOfString:opt2Html]) {
        NSLog(@"opt2WV webview loaded with this  %@ ",opt2Html);
        webViewLoadCount = webViewLoadCount + 1;
    }else if ([string rangeOfString:opt3Html]) {
        NSLog(@"opt3WV webview loaded with this  %@ ",opt3Html);
        webViewLoadCount = webViewLoadCount + 1;
    }else if ([string rangeOfString:opt4Html]) {
        NSLog(@"opt4WV webview loaded with this  %@ ",opt4Html);
        webViewLoadCount = webViewLoadCount + 1;
    }
}

i hope this will help you

0
On

May be helpful

take a NSInteger counter; globally

in viewDidLoad initialise counter = 0;

then

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
 counter++;

 if(counter == 5)
 {
     //All web views has finished loading
     counter = 0; //again reset to 0
 }    
}

Happy coding...