如何检查 UIWebView 是否为空

发布于 2024-12-07 09:20:28 字数 414 浏览 2 评论 0原文

我需要检查加载完成后的网络视图是否有任何内容。

我的要求很简单。它是我页面底部的一个小 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苍暮颜 2024-12-14 09:20:28

如果您正在加载 HTML 页面:

NSString *string = [myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];
BOOL isEmpty = string==nil || [string length]==0;

或者您可以先加载内容,测试它是否不为空,然后将其提供给 webview。请参阅 UIWebView 的 loadHTMLString:baseURL:loadData:MIMEType:textEncodingName:baseURL:

If you are loading a HTML page:

NSString *string = [myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];
BOOL isEmpty = string==nil || [string length]==0;

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: or loadData:MIMEType:textEncodingName:baseURL:.

纸短情长 2024-12-14 09:20:28

这是基于 Jano 的方法,但应该表现更好:

NSString *script = @"document.getElementsByTagName('body')[0].innerHTML.length";
NSString *length = [self.webView stringByEvaluatingJavaScriptFromString:script];

if (length.integerValue > 0) {
    NSLog(@"not empty");
}

This is based on Jano's approach but should perform better:

NSString *script = @"document.getElementsByTagName('body')[0].innerHTML.length";
NSString *length = [self.webView stringByEvaluatingJavaScriptFromString:script];

if (length.integerValue > 0) {
    NSLog(@"not empty");
}
盗梦空间 2024-12-14 09:20:28

只是回复一个旧帖子,但也许新来者会发现它很有用。我遇到了同样的问题,并发现此解决方案适用于我的情况:

if (webView.request.URL.absoluteURL == nil) {
        NSLog(@"nil webby");
        NSLog(@"url: %@", webView.request.URL.absoluteURL);
        // Perform some task when the url is nil
    } else {
        NSLog(@"loaded webby");
        NSLog(@"url: %@", webView.request.URL.absoluteURL);
        // Perform some task when the url is loaded
}

您可以从 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:

if (webView.request.URL.absoluteURL == nil) {
        NSLog(@"nil webby");
        NSLog(@"url: %@", webView.request.URL.absoluteURL);
        // Perform some task when the url is nil
    } else {
        NSLog(@"loaded webby");
        NSLog(@"url: %@", webView.request.URL.absoluteURL);
        // Perform some task when the url is loaded
}

You can call it from the webViewDidFinishLoad: method.

魄砕の薆 2024-12-14 09:20:28

就我而言,我希望检测 PDF 是否格式错误。即,Web 视图将为空,因为无法加载 PDF。从 iOS 7.1.1 开始,我没有得到 didFailLoadWithError 委托回调,我需要一种不同的方法来做到这一点。在尝试将 PDF 文档加载到 Web 视图之前,我最终使用了以下方法。

// See https://developer.apple.com/library/ios/documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_pdf/dq_pdf.html
- (BOOL) isValidPDFDoc: (NSURL *) deviceLocalURL {
    CFStringRef path;
    CFURLRef url;
    CGPDFDocumentRef document;
    size_t count;
    BOOL validDocument = YES;

    // Must use path of URL not absoluteString here.
    path = CFStringCreateWithCString (NULL, [[deviceLocalURL path] cStringUsingEncoding:NSASCIIStringEncoding],
                                      kCFStringEncodingUTF8);
    url = CFURLCreateWithFileSystemPath (NULL, path, // 1
                                         kCFURLPOSIXPathStyle, 0);
    CFRelease (path);
    document = CGPDFDocumentCreateWithURL (url);// 2
    if (!document) {
        validDocument = NO;
    }

    CFRelease(url);
    count = CGPDFDocumentGetNumberOfPages (document);// 3
    if (count == 0) {
        validDocument = NO;
    }

    CGPDFDocumentRelease (document);

    return validDocument;
}

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.

// See https://developer.apple.com/library/ios/documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_pdf/dq_pdf.html
- (BOOL) isValidPDFDoc: (NSURL *) deviceLocalURL {
    CFStringRef path;
    CFURLRef url;
    CGPDFDocumentRef document;
    size_t count;
    BOOL validDocument = YES;

    // Must use path of URL not absoluteString here.
    path = CFStringCreateWithCString (NULL, [[deviceLocalURL path] cStringUsingEncoding:NSASCIIStringEncoding],
                                      kCFStringEncodingUTF8);
    url = CFURLCreateWithFileSystemPath (NULL, path, // 1
                                         kCFURLPOSIXPathStyle, 0);
    CFRelease (path);
    document = CGPDFDocumentCreateWithURL (url);// 2
    if (!document) {
        validDocument = NO;
    }

    CFRelease(url);
    count = CGPDFDocumentGetNumberOfPages (document);// 3
    if (count == 0) {
        validDocument = NO;
    }

    CGPDFDocumentRelease (document);

    return validDocument;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文