Check if UIWebView is loaded

前端 未结 4 370
陌清茗
陌清茗 2021-02-01 21:19

I have an UIWebView in one tab that loads in viewDidLoad, but if user taps other tab the loading will be disrupted and - (void)webView:(UIWebView *)webView didFailLoad

4条回答
  •  醉梦人生
    2021-02-01 21:25

    It is true that the original question was posted many years ago. Recently I had to find a reliable solution to this issue.

    Here is the solution that worked for me:

    1. Replaced UIWebView with WKWebWiew.
    2. Added 'evaluateJavaScript' code while handling the 'didFinishNavigation' delegate method.

    The complete code is:

       - (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
        [webView evaluateJavaScript:@"document.body.innerHTML" completionHandler:^(id result, NSError *error) {
            if (result != nil) {
                // Call your method here
            }
            if(error) {
                NSLog(@"evaluateJavaScript error : %@", error.localizedDescription);
            }
        }];
    }
    

提交回复
热议问题