如何获取在UIWebView中显示的HTML页面的标题?

我需要从显示在UIWebView中的HTML页面中提取标题标签的内容。 什么是最强大的手段呢?

我知道我可以这样做:

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

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

另外,我可以扫描标题的HTML代码的文本,但是如果页面的作者对代码感到不满意,那么感觉有点麻烦,并且可能会变得脆弱。 如果是这样的话,在iPhone API中处理HTML文本的最佳方法是什么?

我觉得我已经忘记了一些明显的东西。 有没有比这两个select更好的方法?

更新:

从这个问题的答案以下: UIWebView:你可以禁用Javascript? 似乎没有办法closuresUIWebView中的Javascript。 因此,上面的Javascript方法将始终工作。

对于那些只是向下滚动才能find答案的人:

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

这将始终工作,因为没有办法closuresUIWebView中的Javascript。

如果启用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来消除<title>的长度,即7

编辑:只是看到你find答案… 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的文档标题是由这是标题的信号所以可能最简单的事情要做的就是search该htmlCodestring的,为和子string,所以我们得到的东西之间。

  //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(这是)的文本,因为位置是string的第一个字符。 所以为了弥补这一点,我们只是添加了string的长度

现在请记住,这个代码没有经过testing..如果有任何问题,它可能是一个拼写错误,或者我没有/没有添加一个指针,当我不应该。

如果标题有点怪,而且不完全正确,可以尝试用NSMakeRange搞乱 – 我的意思是像添加/减去string的不同长度/位置 – 任何看起来合乎逻辑的东西。

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

WKWebView具有“标题”属性,只是这样做,

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

我不认为现在UIWebView是适合的。

我目前还没有使用网页浏览器的经验,但是,我相信这是页面标题的标题,所以,我build议的一个技巧是在webview上使用一个类别,并覆盖self.title的setter,以便添加消息你们中的一个反对或修改一些属性来获得标题。

你可以尝试告诉我,如果它的作品?