2016-02-15 133 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