从网站上静静地获取网站的标题

时间:2014-04-05 01:39:25

标签: ios url uiwebview

我有一个网站的链接,我想得到它的标题。 我尝试用这段代码来做

UIWebView* hiddenWebView;
NSString* urlString = @"http://www.youtube.com/watch?v=OyORxdjGtlk";
            NSURL* url = [NSURL URLWithString:urlString];
            NSURLRequest* request = [NSURLRequest requestWithURL:url];
            [hiddenWebView loadRequest:request];

NSString* text = [hiddenWebView stringByEvaluatingJavaScriptFromString:@"document.title"];

但结果是:text = NULL; 我只是想得到视频的名称

1 个答案:

答案 0 :(得分:1)

UIWebView委托设置为ViewController(例如通过在Interface Builder中按住Ctrl键拖动),然后:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSURL *url = [webView.request mainDocumentURL];
    NSString *str = [url absoluteString];
    NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    NSLog(@"%s: url=%@ str=%@ title=%@", __PRETTY_FUNCTION__, url, str, title);
}