问题
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)?
回答1:
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:
.
回答2:
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");
}
回答3:
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.
回答4:
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;
}
来源:https://stackoverflow.com/questions/7581909/how-does-one-check-if-uiwebview-is-empty-or-not