0

I added a code for Xcode from the internet.

This one:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.youtube.com"]];

It took me to www.youtube.com on the Safari browser, what if I have youtube application? how can it open that from the application in my iPhone?

p.s I’m a newbie

Samia
  • 35
  • 3

2 Answers2

2
- (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];
    }
}
Shehzad Ali
  • 1,846
  • 10
  • 16
  • Yours also worked Thank you! – Samia Feb 15 '16 at 12:36
  • I have one question! If I got a URL which was https://www.youtube.com/watch?v=Hj57OP5C-ls .. what should I add in your code? I'm getting a threat! – Samia Feb 15 '16 at 13:25
  • Do you have only one url to show or there can be multiple urls. I mean you only want to show this video or it can be any video ? You have to format your url as mentioned in the answer. – Shehzad Ali Feb 15 '16 at 14:48
  • This is the only URL to show! – Samia Feb 15 '16 at 15:07
  • Even when I remove it all, I still have a thread. – Samia Feb 15 '16 at 15:26
  • Replace 1st two lines of my method with these two lines. – Shehzad Ali Feb 15 '16 at 15:27
  • Try this: `NSURL *linkToAppURL = [NSURL URLWithString:@"youtube://watch?v=Hj57OP5C-ls"]; ` `NSURL *linkToWebURL = [NSURL URLWithString:@"https://www.youtube.com/watch?v=Hj57OP5C-ls"];` – Shehzad Ali Feb 15 '16 at 15:29
  • This one : Unused variable 'linkToAppURL' and this one :Unused variable 'linkToWebURL' – Samia Feb 15 '16 at 15:30
  • `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]; }` – Shehzad Ali Feb 15 '16 at 15:32
  • you declared the variable but you aren't using them for opening app – Shehzad Ali Feb 15 '16 at 15:33
  • I removed everything for the link, it shows me the same problem on the button :( – Samia Feb 15 '16 at 15:34
  • Removed everything means? Code is still there in the answer – Shehzad Ali Feb 15 '16 at 15:36
  • Yes but I still have a problem when I removed the like http://oi66.tinypic.com/261f22g.jpg – Samia Feb 15 '16 at 15:45
  • This is SIGABRT. I can't see your code. – Shehzad Ali Feb 15 '16 at 15:50
  • I removed the button and made another one, it works well, the thing is, I don't know what to change in the brown ones, in the very first code, my iPhone works fine but .. what if it was https://www.youtube.com/watch?v=Hj57OP5C-ls? – Samia Feb 15 '16 at 16:28
  • In the two thngs @"youtube://user/%@",@"toyotaleasing" @"http://www.youtube.com/user/%@",@"toyotaleasing I don't know what to add! – Samia Feb 15 '16 at 16:35
  • I can't msg there but it worked :) – Samia Feb 15 '16 at 16:49
0

Use URL scheme for youtube : youtube://

Eg.

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"youtube://www.youtube.com/watch?v=<video-id>"]]) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"youtube://www.youtube.com/watch?v=<video-id>"]];
}

Hope, it'll help you.

Manann Sseth
  • 2,745
  • 2
  • 30
  • 50