iosobjective-cxcodehyperlink

How can I open the link on an application?


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


Solution

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