Anand Anand - 4 months ago 71
iOS Question

how does one check if UIWebView is empty or not

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

Answer

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

Comments