如何检查 UIWebView 是否为空
我需要检查加载完成后的网络视图是否有任何内容。
我的要求很简单。它是我页面底部的一个小 webview 条(如广告),
我称之为
NSURLRequest *request=[NSURLRequest requestWithURL:adURL];
[gWebView loadRequest:request];
回调,
-(void)webViewDidFinishLoad:(UIWebView *)webView {
但在我的场景中,webview 将返回空,有时它应该有数据。
如果我的服务器 php 文件没有返回任何内容,我不想显示 webview。
如何验证我在回调中(或任何其他方式)收到了一个空页面?
I need to check if a webview when completed loading has any content or not.
What I require is simple. Its a small webview strip at the bottom of my pages (like an advert)
I call
NSURLRequest *request=[NSURLRequest requestWithURL:adURL];
[gWebView loadRequest:request];
I get the callback
-(void)webViewDidFinishLoad:(UIWebView *)webView {
But in my scenario the webview shall return empty and sometime it shall have data.
I do not want to show the webview if my server php file returned nothing.
How can I verify that I received an empty page in the callback (or any other way)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您正在加载 HTML 页面:
或者您可以先加载内容,测试它是否不为空,然后将其提供给 webview。请参阅
UIWebView 的 loadHTMLString:baseURL:
或loadData:MIMEType:textEncodingName:baseURL:
。If you are loading a HTML page:
Or you could load the content first, test if it is not empty, and then feed it to the webview. See
UIWebView's loadHTMLString:baseURL:
orloadData:MIMEType:textEncodingName:baseURL:
.这是基于 Jano 的方法,但应该表现更好:
This is based on Jano's approach but should perform better:
只是回复一个旧帖子,但也许新来者会发现它很有用。我遇到了同样的问题,并发现此解决方案适用于我的情况:
您可以从 webViewDidFinishLoad: 方法调用它。
Just replying to an old post, but maybe new arrivals will find it useful. I struggled with the same problem and found this solution to work in my case:
You can call it from the webViewDidFinishLoad: method.
就我而言,我希望检测 PDF 是否格式错误。即,Web 视图将为空,因为无法加载 PDF。从 iOS 7.1.1 开始,我没有得到 didFailLoadWithError 委托回调,我需要一种不同的方法来做到这一点。在尝试将 PDF 文档加载到 Web 视图之前,我最终使用了以下方法。
In my case, I was looking to detect if the PDF was malformed. I.e., the web view would be empty because the PDF could not be loaded. Since with iOS 7.1.1, I wasn't getting the didFailLoadWithError delegate callback, I needed a different way to do this. I ended up using the method below before attempting to load the PDF doc into the web view.