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
How to detect when a UIWebView has completely finished loading?
提问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 UIWebViewDelegate
protocol method webViewDidFinishLoad
and webView's isLoading property
使用UIWebViewDelegate
协议方法webViewDidFinishLoad
和webView'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:
这是对我有用的解决方案:
- Replaced UIWebView with WKWebWiew.
- Added 'evaluateJavaScript' code while handling the 'didFinishNavigation' delegate method.
- 用 WKWebWiew 替换了 UIWebView。
- 在处理“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 UIWebView
and as a result you don't have an access to webViewDidFinishLoad
but you have a reference to UIWebView
you 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
}