how does one check if UIWebView is empty or not

前端 未结 4 733
醉话见心
醉话见心 2021-02-15 17:02

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

相关标签:
4条回答
  • 2021-02-15 17:50

    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");
    }
    
    0 讨论(0)
  • 2021-02-15 17:54

    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;
    }
    
    0 讨论(0)
  • 2021-02-15 17:56

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

    0 讨论(0)
  • 2021-02-15 17:58

    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 讨论(0)
提交回复
热议问题