我应该在超链接中添加什么?

时间:2016-02-15 14:03:05

标签: ios objective-c xcode hyperlink

如果我的网址是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 个答案:

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

答案 1 :(得分: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"]]