ios 如何检测 UIWebView 何时完全加载完成?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25445451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:00:06  来源:igfitidea点击:

How to detect when a UIWebView has completely finished loading?

iosobjective-cuiwebviewuiwebviewdelegate

提问by ORStudios

I am trying to build a filter for a UIWebView and I am struggiling to detect when the UIWebView has completely finished loading. I have used the following two methods

我正在尝试为 UIWebView 构建过滤器,并且我正在努力检测 UIWebView 何时完全完成加载。我使用了以下两种方法

– webView:shouldStartLoadWithRequest:navigationType:
– webViewDidFinishLoad:

but the issue is that these will be called multiple times when a page has frames and additional content to load.

但问题是当页面有框架和其他内容要加载时,这些将被多次调用。

What I need is to know when the view has completely loaded and there is no more content to fetch. Then when the content has loaded I can check to URL of the page against a list of approved URLS.

我需要的是知道视图何时完全加载并且没有更多内容可以获取。然后当内容加载时,我可以根据批准的 URL 列表检查页面的 URL。

ANy ideas?

有任何想法吗?

回答by Paresh Navadiya

Use the UIWebViewDelegateprotocol method webViewDidFinishLoadand webView's isLoading property

使用UIWebViewDelegate协议方法webViewDidFinishLoadwebView's isLoading property

 - (void)webViewDidFinishLoad:(UIWebView *)webView
 {
    //Check here if still webview is loding the content
    if (webView.isLoading)
       return;

    //after code when webview finishes
    NSLog(@"Webview loding finished");
 }

回答by realtimez

Swift 3 version:

斯威夫特 3 版本:

func webViewDidFinishLoad(_ webView: UIWebView) {
    if webView.isLoading{
        return
    }
    print("Done loading")
}

回答by Love Thailand

Try use:

尝试使用:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)requestURL navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [requestURL URL];
NSLog(@"##### url = %@",[url absoluteString]); 
return YES;
}

don't forget to set your UIWebView Delegate

不要忘记设置您的 UIWebView 委托

or add statement,

或添加语句,

NSRange range = [[url absoluteString] rangeOfString:@"https://www.google.com"];
if (range.location != NSNotFound)
{}

hope to help you.

希望能帮到你。

回答by Nish

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.
  1. 用 WKWebWiew 替换了 UIWebView。
  2. 在处理“didFinishNavigation”委托方法时添加了“evaluateJavaScript”代码。

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);
        }
    }];
}

回答by yoAlex5

If you aren't an owner of UIWebViewand as a result you don't have an access to webViewDidFinishLoadbut you have a reference to UIWebViewyou can use the next code

如果您不是所有者,UIWebView因此您无权访问,webViewDidFinishLoad但您有参考,则UIWebView可以使用下一个代码

Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { (timer) in

    guard uiWebView.isLoading else {
        return
    }

    timer.invalidate()
    //Add your logic here

}