我有一个网站的链接,我想得到它的标题。 我尝试用这段代码来做
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; 我只是想得到视频的名称
答案 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);
}