WebViewDidFinishLoad is called multiple times for a single loadrequest

I have successfully used the webView delegate because of the long time. But recently I came across this issue with this delegation. In my current project, I am trying to access my router from webview. I skip the username and password only inside the URL. Below is the download request code.

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://uname: password@192.168.1.1 "]]]; 

This calls the webView delegate method (webViewDidFinishLoad and webViewDidStartLoad) 5 times. Expected? When I pass in a simple url like google.com, it works as expected. But with username and password, why are these delegate methods called 5 times?

If this is correct, then I need to know why it only calls 5 times. The reason is that in my program I call performSegueWithIdentifier in the webViewDidFinishLoad method, in which case it calls segue 5 times. For a workaround, I can maintain the counter and only call performSegueWithIdentifier on the 5th counter.

thanks

+6
source share
3 answers

webViewDidStartLoad/webViewDidFinishLoad is called once for each HTML frame. Your content may have several frames.

See UIWebViewDelegate Docs .

webViewDidStartLoad: Dispatched after the web view starts loading the frame.

+14
source

These methods work for me ... :)

 #pragma mark UI Web View Delegate NSInteger webViewLoads; //a web view starts loading - (void)webViewDidStartLoad:(UIWebView *)webView{ webViewLoads++; [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; [SVProgressHUD showWithStatus:@"Loading..." maskType:SVProgressHUDMaskTypeBlack]; } //web view finishes loading - (void)webViewDidFinishLoad:(UIWebView *)webView{ webViewLoads--; [self performSelector:@selector(webViewFinishLoadWithCondition) withObject:nil afterDelay:0.5]; } //web view handling error - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{ webViewLoads--; NSLog(@"Web View Did Fail Load With Error : %@",error); [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; [SVProgressHUD dismiss]; } -(void)webViewFinishLoadWithCondition{ if(webViewLoads==0){ [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; [SVProgressHUD dismiss]; } } 
+6
source

How webViewDidStartLoad/webViewDidFinishLoad is called once for each HTML frame. Use an integer to find when the last frame has finished loading,

More details:

  • Integer increment in webViewDidStartLoad
  • Decrease integer in webViewDidFinishLoad
  • In webViewDidFinishLoad check when the integer is zero, which means that all frames of the webpage are loaded, Now call the selector

This is an explanation of @marvin's answer



The best approach:

Check isLoading in webViewDidFinishLoad and isLoading for false, do what you want

 -(void)webViewDidFinishLoad:(UIWebView *)webview { if (!webview.isLoading) { // webview is done loading content. // `performSegueWithIdentifier` / Your code Here } } 
0
source

Source: https://habr.com/ru/post/946777/


All Articles