我需要从 UIWebView 中显示的 HTML 页面中提取标题标签的内容.这样做最可靠的方法是什么?
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: Can You Disable 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 页面的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!