2014-04-05 84 views
0

我有一個網站的鏈接,我想要獲得它的標題。 我試圖通過這個代碼從一個網站默默獲取網站的標題

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"]; 

做但結果是:文本= NULL; 我只是想獲得視頻

+0

YouTube有一個[API](HTTPS ://developers.google.com/youtube/),您可以使用它來找出視頻標題。 –

+0

我知道。但我的應用不僅有youtube鏈接,而且還有其他網頁的鏈接 –

回答

1

(按Ctrl拖動界面生成器爲例)UIWebView委託設爲您的視圖控制器的名稱,然後:

- (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); 
}