views:

708

answers:

1

I need to extract the contents of the title tag from an HTML page displayed in a UIWebView. What is the most robust means of doing so?

I know I can do:

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

However, that only works if javascript is enabled.

Alternatively, I could just scan the text of the HTML code for the title but that feels a bit cumbersome and might prove fragile if the page's authors got freaky with their code. If it comes to that, what's the best method to use for processing the html text within the iPhone API?

I feel that I've forgotten something obvious. Is there a better method than these two choices?

Update:

Following from the answer to this question: UIWebView: Can You Disable Javascript? there appears to be no way to turn off Javascript in UIWebView. Therefore the Javascript method above will always work.

+1  A: 

I pretty sure those are the only options you have. UIWebView does not expose any document information directly.

Ole Begemann
You're probably right. Do you have any parsing suggestions?
TechZen
AFAIK, libxml2 has a HTML parsing mode.
Ole Begemann
Yeah, I was hoping for something simpler. I guess I can just scan for the tag and hope the html is standard.
TechZen
@TechZen - be very very careful with this: the webview never guarantees that it's rendering HTML.
Dave DeLong
Good point. That is in part why I wanted to avoid parsing at all. You never know what your going to get for input.
TechZen