2013-08-26 58 views
3

對於在youtube應用上啓動視頻,我使用的是以下代碼。在youtube應用中啓動youtube頻道

NSURL *instagramURL = [NSURL URLWithString:@"youtube://foo"]; 
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) { 
    NSLog(@"opening youtube app..."); 
    NSString *stringURL = @"http://www.youtube.com/watch?v=H9VdapQyWfg"; 
    NSURL *url = [NSURL URLWithString:stringURL]; 
    [[UIApplication sharedApplication] openURL:url]; 
} else { 
    // open in UIWebView in WebViewViewController 
    WebViewViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"webinterface"]; 

    secondView.headerLabel = @"YouTube"; 
    secondView.webPath = @"http://www.youtube.com/watch?v=H9VdapQyWfg"; 

    [self.navigationController pushViewController:secondView animated:YES]; 
} 

現在客戶端改變了主意,並要求在iPhone應用程序中放置頻道。

爲了進行測試,我用的鏈接http://www.youtube.com/user/richarddawkinsdotnet

但是當我使用這個鏈接而不是YouTube應用,它總是在Safari中打開。 :(

我如何能在YouTube應用打開通道與提供的鏈接任何想法/建議?

回答

12

你的代碼是怎麼回事錯誤的,因爲,儘管你檢查是否可以打開youTube網址或多或少地正確,然後你只需打開一個網址,該網址將永遠在Safari中打開。

這是我剛纔使用的代碼,它可以幫助我。 else語句,如果你想回退到使用webviewcontroller,因爲這將打開Safari,如果y沒有安裝outube應用程序。

NSString *channelName = @"TheNameOfTheChannel"; 

NSURL *linkToAppURL = [NSURL URLWithString:[NSString stringWithFormat:@"youtube://user/%@",channelName]]; 
NSURL *linkToWebURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.youtube.com/user/%@",channelName]]; 

if ([[UIApplication sharedApplication] canOpenURL:linkToAppURL]) { 
    // Can open the youtube app URL so launch the youTube app with this URL 
    [[UIApplication sharedApplication] openURL:linkToAppURL]; 
} 
else{ 
    // Can't open the youtube app URL so launch Safari instead 
    [[UIApplication sharedApplication] openURL:linkToWebURL]; 
} 
+0

我會檢查並送還給你......我從來沒有發現這個答案。希望這作品..:D:P –

+0

這不適合我!它只是去應用程序的視頻頁面,並說「播放錯誤點擊重試」,但在safari中打開時工作正常。 – maxisme

+0

如果你想在本地應用程序中打開URL,你應該這樣使用: NSURL * linkToAppURL = @「youtube://www.youtube.com/user/%@」,channelName]; ..... 下面的方案不會工作NSURL * linkToAppURL = @「youtube:// user /%@」,channelName]; –

1

相同的答案,但更短:

if (![[UIApplication sharedApplication] openURL: [NSURL URLWithString: @"youtube://user/%channelName%"]]) { 
    NSURL *webURL = [NSURL URLWithString:@"http://www.youtube.com/user/%channelName%"]; 
    [[UIApplication sharedApplication] openURL: webURL]; 
} 

和Twitter:

if (![[UIApplication sharedApplication] openURL: [NSURL URLWithString: @"twitter://user?screen_name=%channelName%"]]) { 
    NSURL *webURL = [NSURL URLWithString:@"http://twitter.com/%channelName%"]; 
    [[UIApplication sharedApplication] openURL: webURL]; 
} 

這裏是應用程序的更詳盡的列表:

http://wiki.akosma.com/IPhone_URL_Schemes

0

youtube://user/<channel-id>停在iOS 9,所以我研究工作,發現了這個現在的工作以及在iOS版9

youtube://www.youtube.com/channel/<channel-id>