8

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)?

Anand
  • 4,182
  • 6
  • 42
  • 54

4 Answers4

14

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:.

new2ios
  • 1,350
  • 2
  • 25
  • 56
Jano
  • 62,815
  • 21
  • 164
  • 192
  • This works but it returns "" in the string, although I could make it work by checking if I get that in the string. Thanks for the help. – Anand Sep 28 '11 at 10:52
  • 5
    Sorry for the "Comment-after-a-year thing", but this might help. Just change ('html') to ('body') and it should work well. – Shai Mishali Aug 28 '12 at 16:33
  • 1
    I was trying to use this to detect if a malformed PDF file had been loaded (or, actually, not loaded), and neither "body" nor "html" work for this purpose. Interestingly, on iOS7, the delegate method, didFailLoadWithError doesn't get called if the PDF document is malformed. – Chris Prince Jun 23 '14 at 22:49
6

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");
}
Blago
  • 4,697
  • 2
  • 34
  • 29
4

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.

0

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;
}
Chris Prince
  • 7,288
  • 2
  • 48
  • 66