how does one check if UIWebView is empty or not

前端 未结 4 734
醉话见心
醉话见心 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: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;
    }
    

提交回复
热议问题