【问题标题】:How to Get the Title of a HTML Page Displayed in UIWebView?如何获取在 UIWebView 中显示的 HTML 页面的标题?
【发布时间】:2011-01-17 13:36:15
【问题描述】:

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

我知道我能做到:

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

但是,这仅在启用 javascript 时有效。

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

我觉得我忘记了一些明显的事情。有比这两种选择更好的方法吗?

更新:

根据这个问题的答案:UIWebView: Can You Disable Javascript? 似乎没有办法在 UIWebView 中关闭 Javascript。因此,上面的 Javascript 方法将始终有效。

【问题讨论】:

标签: ios objective-c iphone uiwebview foundation


【解决方案1】:

如果您的代码中经常需要它,我建议您像这样将 func 添加到“扩展 UIWebView”中

extension UIWebView {

    func title() -> String {
        let title: String = self.stringByEvaluatingJavaScript(from: "document.title")!
        return title
    }
}

或者最好使用 WKWebView。

不幸的是,它在 ARKit 中没有得到很好的支持。我不得不放弃 WKWebView。我无法将网站加载到 webView 中。如果有人对this issue here 有解决方案 -> 我有一个类似的问题,那将有很大帮助。

【讨论】:

    【解决方案2】:

    这里是 Swift 4 版本,基于 here 的回答

    func webViewDidFinishLoad(_ webView: UIWebView) {
        let theTitle = webView.stringByEvaluatingJavaScript(from: "document.title")
    }
    

    【讨论】:

      【解决方案3】:

      WKWebView 有 'title' 属性,就这样吧,

      func webView(_ wv: WKWebView, didFinish navigation: WKNavigation!) {
          title = wv.title
      }
      

      我认为UIWebView 现在不适合。

      【讨论】:

        【解决方案4】:

        如果启用了 Javascript 使用这个:-

        NSString *theTitle=[webViewstringByEvaluatingJavaScriptFromString:@"document.title"];
        

        如果禁用 Javascript 使用这个:-

        NSString * htmlCode = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.appcoda.com"] encoding:NSASCIIStringEncoding error:nil];
        NSString * start = @"<title>";
        NSRange range1 = [htmlCode rangeOfString:start];
        
        NSString * end = @"</title>";
        NSRange range2 = [htmlCode rangeOfString:end];
        
        NSString * subString = [htmlCode substringWithRange:NSMakeRange(range1.location + 7, range2.location - range1.location - 7)];
        NSLog(@"substring is %@",subString);
        

        我在 NSMakeRange 中使用 +7 和 -7 来消除 &lt;title&gt; 的长度,即 7

        【讨论】:

        • 现在可以在 UIWebview 中禁用 Javascript 吗?早在 2010 年你就做不到。
        • 我不知道 IOS 8 之前的 IOS 但你可以做到,转到设置 --> Safari --> 高级 --> Javascript On/Off
        • 如何知道代码中的javascript是启用还是禁用?
        【解决方案5】:

        编辑:刚刚看到你找到了答案... sheeeiiitttt

        我真的刚刚学会了这个!为此,您甚至不需要将其显示在 UIWebView 中。 (但是在你使用的时候,你可以直接获取当前页面的URL)

        无论如何,这是代码和一些(微弱的)解释:

            //create a URL which for the site you want to get the info from.. just replace google with whatever you want
            NSURL *currentURL = [NSURL URLWithString:@"http://www.google.com"];
            //for any exceptions/errors
            NSError *error;
            //converts the url html to a string
            NSString *htmlCode = [NSString stringWithContentsOfURL:currentURL encoding:NSASCIIStringEncoding error:&error];
        

        所以我们有了 HTML 代码,现在我们如何获得标题?好吧,在每个基于 html 的文档中,标题都由 This Is the Title 表示 所以可能最简单的做法是在 htmlCode 字符串中搜索 、 for 和子字符串,以便我们得到介于两者之间的内容。

            //so let's create two strings that are our starting and ending signs
            NSString *startPoint = @"<title>";
            NSString *endPoint = @"</title>";
            //now in substringing in obj-c they're mostly based off of ranges, so we need to make some ranges
            NSRange startRange = [htmlCode rangeOfString:startPoint];
            NSRange endRange = [htmlCode rangeOfString:endPoint];
            //so what this is doing is it is finding the location in the html code and turning it
            //into two ints: the location and the length of the string
            //once we have this, we can do the substringing!
            //so just for easiness, let's make another string to have the title in
            NSString *docTitle = [htmlString substringWithRange:NSMakeRange(startRange.location + startRange.length, endRange.location)];
            NSLog(@"%@", docTitle);
            //just to print it out and see it's right
        

        真的是这样! 所以基本上要解释 docTitle 中发生的所有恶作剧,如果我们只是通过说 NSMakeRange(startRange.location, endRange.location) 来创建一个范围,我们将获得 startString 的标题和文本(即 ),因为位置是字符串的第一个字符。 所以为了抵消它,我们只是添加了字符串的长度

        现在请记住,此代码未经测试。如果有任何问题,可能是拼写错误,或者我没有/确实添加了不应该添加的指针。

        如果标题有点奇怪且不完全正确,请尝试使用 NSMakeRange ——我的意思是添加/减去字符串的不同长度/位置——任何看起来合乎逻辑的东西。

        如果您有任何问题或有任何问题,请随时提出。这是我在这个网站上的第一个答案,如果有点混乱,很抱歉

        【讨论】:

          【解决方案6】:

          到目前为止,我没有使用 webviews 的经验,但是,我相信它会将它的标题设置为页面标题,所以,我建议的一个技巧是在 webview 上使用一个类别并覆盖 self.title 的设置器,以便添加向你们中的一个人发送消息,或修改某些属性以获得标题。

          你能告诉我它是否有效吗?

          【讨论】:

            【解决方案7】:

            对于那些只是向下滚动以找到答案的人:

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

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

            【讨论】:

            • 使用这个,如果网页的编码不是'utf-8',标题会很乱。
            • Swift 4 怎么样?
            猜你喜欢
            • 2015-11-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-05-02
            • 2010-11-06
            相关资源
            最近更新 更多