如何获取UIWebView中显示的HTML页面的标题? [英] How to Get the Title of a HTML Page Displayed in UIWebView?

查看:111
本文介绍了如何获取UIWebView中显示的HTML页面的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从UIWebView中显示的HTML页面中提取title标签的内容。这样做最有力的方法是什么?

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?

我知道我能做到:

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

但是,只有在启用了javascript的情况下才有效。

However, that only works if javascript is enabled.

或者,我可以只扫描标题的HTML代码文本,但感觉有点麻烦,如果页面的作者对他们的代码变得怪异,可能会很脆弱。如果是这样,用于处理iPhone API中的html文本的最佳方法是什么?

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?

从这个问题的答案开始: UIWebView:你能禁用Javascript吗?似乎无法关闭UIWebView中的Javascript。因此,上面的Javascript方法将始终有效。

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.

推荐答案

对于那些只需向下滚动查找答案的人:

For those who just scroll down to find the answer:

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

这将始终有效,因为无法在UIWebView中关闭Javascript。

This will always work as there is no way to turn off Javascript in UIWebView.

这篇关于如何获取UIWebView中显示的HTML页面的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆