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
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;
}