如何将Youtube视频网址转换为iPhone应用程序

时间:2013-01-31 07:30:52

标签: iphone facebook ios5 uiwebview youtube

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

我正在使用以下代码

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

1 个答案:

答案 0 :(得分: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);