iOS App - Set Timeout for UIWebView loading

后端 未结 4 1561
小蘑菇
小蘑菇 2021-01-30 14:52

I have a simple iOS native app that loads a single UIWebView. I would like the webView to show an error message if the app doesn\'t COMPLETELY finish loading the initial page in

4条回答
  •  天命终不由人
    2021-01-30 15:21

    My way is similar to accepted answer but just stopLoading when time out and control in didFailLoadWithError.

    - (void)timeout{
        if ([self.webView isLoading]) {
            [self.webView stopLoading];//fire in didFailLoadWithError
        }
    }
    
    - (void)webViewDidStartLoad:(UIWebView *)webView{
        self.timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(timeout) userInfo:nil repeats:NO];
    }
    
    - (void)webViewDidFinishLoad:(UIWebView *)webView{
        [self.timer invalidate];
    }
    
    - (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{
        //Error 999 fire when stopLoading
        [self.timer invalidate];//invalidate for other errors, not time out. 
    }
    

提交回复
热议问题