Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
possible duplicate of : stackoverflow.com/questions/1842370/… –  Rajan Balana Jun 7 '13 at 12:47

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
Thanks TomSwift. My web page has 4 frames. Here is more info about html frames - w3.org/TR/html401/present/frames.html –  Ashish Jun 13 '13 at 16:04

This Methods works for me... :)

#pragma mark UI Web View Delegate
int 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];
    }
}
share|improve this answer
1  
This could use some more explanation. How exactly does it solve the problem? –  Andrew Medico May 14 at 4:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.