Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webViewDidFinishLoad called multiple times for single loadrequest

Tags:

ios

uiwebview

I have been using webView delegate successfully from long time. But recently I faced strange issue with this delegate. In my current project I am trying to access my router from webview. I am passing username and password inside URL only. Below is load request code.

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

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

If this behaviour is correct then I need to know why it calls 5 times only. The reason is, in my program - I am calling performSegueWithIdentifier in webViewDidFinishLoad method and in present form it calls segue 5 times. For workaround I can maintain count and will call performSegueWithIdentifier on 5th count only.

Thanks

like image 828
Ashish Avatar asked Jun 07 '13 12:06

Ashish


2 Answers

webViewDidStartLoad/webViewDidFinishLoad are called once per HTML frame. Your content likely has multiple frames in it.

See UIWebViewDelegate docs.

webViewDidStartLoad: Sent after a web view starts loading a frame.

like image 109
TomSwift Avatar answered Oct 15 '22 07:10

TomSwift


This Methods works 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];
    }
}
like image 6
marvin Avatar answered Oct 15 '22 07:10

marvin