Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop webViewDidFinishLoad from being called rapidly

In my webViewDidFinishLoad method, I have the following code:

- (void)webViewDidFinishLoad:(UIWebView *)webView {   
    if (hasPressed == 1) {
        hasPressed = 0;
        isBlocked = 0;
    }
    else if (hasPressed == 0 && hasDroppedDown == 0) {
        if (viewState != kStateWeather || isBlocked == 0) {
        [UIView animateWithDuration:0.5 
                              delay:0.0 options:UIViewAnimationCurveLinear animations:^{
                                  //Animations
                              }
                         completion:^(BOOL finished){
                             hasDroppedDown = 1;
                         }];
        }
    }
}

As you can see, if this method is called rapidly, e.g. a website that has a redirect, the else in my else if becomes useless. How do I stop it from being called so rapidly?

like image 977
Conor Taylor Avatar asked Dec 05 '25 10:12

Conor Taylor


2 Answers

Unfortunately, you cant do that. It will call when ever a new URL is start loading in the UIWebView. Instead you can do one thing. Implement the delegate function

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

This delegate will be called when a new frame is about to start loading in the UIWebView. You can figure out which type of request is going to load by checking the navigationType which can attain values

UIWebViewNavigationTypeLinkClicked
UIWebViewNavigationTypeFormSubmitted
UIWebViewNavigationTypeBackForward
UIWebViewNavigationTypeReload
UIWebViewNavigationTypeFormResubmitted
UIWebViewNavigationTypeOther

Note: return YES if you allow the webview to load the page else NO.

Hope this help you.

like image 50
Mathew Varghese Avatar answered Dec 08 '25 01:12

Mathew Varghese


- (void)main { //being called every 0.2
    t+=0.2;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {   
    loadImg.hidden = 1;
    m_activity.hidden= TRUE;     
    [m_activity stopAnimating];
    if (t>0.5) {
        [self finishedLoading];
        t=0;
    }
}
- (void)finishedLoading {
    if (hasPressed == 1) {
        hasPressed = 0;
        isBlocked = 0;
    }
    else if (hasPressed == 0 && hasDroppedDown == 0) {
        if (viewState != kStateWeather || isBlocked == 0) {
            [UIView animateWithDuration:0.5 
                                  delay:0.0 options:UIViewAnimationCurveLinear animations:^{
                                      //Animations
                                  }
                             completion:^(BOOL finished){
                                 hasDroppedDown = 1;
                             }];
        }
    }
}
like image 43
Conor Taylor Avatar answered Dec 07 '25 23:12

Conor Taylor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!