2016-02-15 127 views
1

如果我的网址是youtube.com/watch?v=Hj57OP5C-ls ..我应该在代码中添加什么内容?我应该添加到超链接?

- (IBAction)btnYoutube:(id)sender { 

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

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

回答

2

做到这一点:-)

- (IBAction)btnYoutube:(id)sender { 

    NSURL *linkToAppURL = [NSURL URLWithString:@"youtube://watch?v=Hj57OP5C-ls"]; 
    NSURL *linkToWebURL = [NSURL URLWithString:@"https://www.youtube.com/watch?v=Hj57OP5C-ls"]; 

    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

刚刚做了,相同,但认为它工作! :) – Samia

+0

伟大:)快乐编码:) –

0

试试这个,

NSURL *linkToAppURL = [NSURL URLWithString:[NSString stringWithFormat:@"youtube://video/%@",@"Hj57OP5C-ls"]]; 
NSURL *linkToWebURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.youtube.com/watch?v=%@",@"Hj57OP5C-ls"]] 
+0

它给了我两胁:未使用的变量 'linkToAppURL' 和未使用的变量 'linkToWebURL' – Samia

+0

@Samia:为什么它显示未使用的变量。因为它用于检查条件。 –

+0

我有点不知所措,你能告诉我更多吗? – Samia