9

I'm creating an application where users can share data by sending emails to each other. In this mail there is a link to my app (I created an URL scheme for my app) But, if the users doesn't have my application on his iPhone, then when he clicks on the link, nothing happens.

My question : How could I do to launch my app when the application is installed on the user's iPhone and launch the Appstore or a website if this application is not installed

Thank you

Titouan de Bailleul
  • 12,920
  • 11
  • 66
  • 121

1 Answers1

6

canOpenURL is what you're looking for.

Here is an example for launching twitter app:

NSURL* twitterURL = [NSURL URLWithString:[NSString stringWithFormat:@"twitter:@%@", twitterUser]];

if ([[UIApplication sharedApplication] canOpenURL:twitterURL]) {
    [[UIApplication sharedApplication] openURL:twitterURL];
} else {
    NSURL* url = [NSURL URLWithString:@"http://itunes.apple.com/us/app/twitter/id333903271?mt=8"];
    [[UIApplication sharedApplication] openURL:url];
}   

EDIT

If you want do do this from a webpage or html email, you find your answer here: https://stackoverflow.com/a/627942/550177

and here: https://stackoverflow.com/a/1109200/550177

Community
  • 1
  • 1
Felix
  • 35,354
  • 13
  • 96
  • 143
  • 1
    OP is asking about a link in Mail.app. – paulmelnikow May 11 '12 at 21:08
  • I tried the two links but I can't make window.location = ... to work. Are you sure it works in emails ? – Titouan de Bailleul May 12 '12 at 09:30
  • No, I don't know how to embed JavaScript Code in HTML emails. Sorry. – Felix May 13 '12 at 19:51
  • anybody found the answer to this? In my case if the URL scheme is not in my plist, the link (myapp://?aParam=123) is not even detected as a link. It appears as normal text. I want, if the app is not installed, take the user to the appstore. – nr5 Jan 28 '15 at 09:01
  • embed a http link that redirects the user either to the app store or the app using that javascript trick (see http://stackoverflow.com/a/1109200/550177 ). – Felix Jan 28 '15 at 11:39