0

我正在开发一个 iPhone 应用程序,在其中将 youtube 链接添加到 UIWebView。现在,当我单击任何视频播放该视频的链接时,我需要检索该链接,以便用户可以在 Facebook 上共享该链接。

我正在使用以下代码

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL  URLWithString:@"http://m.youtube.com"]]];
}
4

1 回答 1

0

使用此代码可能会对您有所帮助

NSString *getURL = @"";

    if (userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
        getURL = [webview stringByEvaluatingJavaScriptFromString:@"function getURL() {var player = document.getElementById('player'); var video = player.getElementsByTagName('video')[0]; return video.getAttribute('src');} getURL();"];
    } else {
        getURL = [webview stringByEvaluatingJavaScriptFromString:@"function getURL() {var bh = document.getElementsByClassName('bh'); if (bh.length) {return bh[0].getAttribute('src');} else {var zq = document.getElementsByClassName('zq')[0]; return zq.getAttribute('src');}} getURL();"];
    }

    NSString *getTitle = [webview stringByEvaluatingJavaScriptFromString:@"function getTitle() {var jm = document.getElementsByClassName('jm'); if (jm.length) {return jm[0].innerHTML;} else {var lp = document.getElementsByClassName('lp')[0]; return lp.childNodes[0].innerHTML;}} getTitle();"];

    NSString *getTitleFromChannel = [webview stringByEvaluatingJavaScriptFromString:@"function getTitleFromChannel() {var video_title = document.getElementById('video_title'); return video_title.childNodes[0].innerHTML;} getTitleFromChannel();"];

    NSLog(@"%@, %@, %@", getURL, getTitle, getTitleFromChannel);
于 2013-01-31T08:05:13.997 回答