How to Get the Title of a HTML Page Displayed in UIWebView?(如何获取在 UIWebView 中显示的 HTML 页面的标题?)
问题描述
我需要从 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 页面的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获取在 UIWebView 中显示的 HTML 页面的标题?
基础教程推荐
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
