8

Is it possible to somehow launch my app using a URL sent via mail? For example I have User profile that user wants to invite their friend into the app. They send an e-mail that has some url like:

join me via this link: http://appname?sender_id=25&some_other_value=something

and opening that link from the iPhone would bring user into the application and would let me parse those values.

Is that possible?

Eugene
  • 10,006
  • 4
  • 37
  • 55
  • 3
    Yes, it's called a [custom URL scheme](http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/AdvancedAppTricks/AdvancedAppTricks.html#//apple_ref/doc/uid/TP40007072-CH7-SW50). – ceejayoz Mar 25 '13 at 18:05
  • @ceejayoz Thanks, post it as an answer and I'll accept it. – Eugene Mar 25 '13 at 18:12
  • @Eugene I am also facing the same issue. Can you tell me how have you solved? I got Lefteris solution but how do I redirect from Safari to app? Where to call that .html file? – Krunal Dec 24 '13 at 09:37
  • @Goti Send a custom URL scheme link to user's email and have them open it on iPhone – Eugene Dec 24 '13 at 12:15
  • 1
    @Eugene Do I need to create "appInstalled" page in my application website? Because, for Android, it doesn't require this thing. – Krunal Dec 26 '13 at 06:55

3 Answers3

23

Yes, this is totally possible. You need to register a URL scheme for your app.

Select your app project in Xcode, then click on the target, and from the Info tab, register a new URL scheme.

The identifier is your app identifier as com.company.AppName and the URL Scheme is the name you wish to use, like appName

Now as for the ideal solution, as we are adding this to our app now, you should ideally NOT send links in email with your custom scheme. The reason is that the user might open it from a computer, so this link will not work.

The best scenario is the following:

  1. When your app is run for the First time, open from withing your app the Safari browser and send it to your website.
  2. In the website, install a cookie for Safari (like myAppIsInstalled)
  3. In the same website, kick the user back to your app, by just redirecting him to your app with your custom URL scheme, like appname://

Now you send in your emails the links with normal URL's that link to your website and here comes part 2:

  1. In your website you check if your app is installed (the cookie is present)
  2. If it's present, instead of opening the link from your website, redirect the user to your app, with the proper values, like

    appname://mailbox?sender_is=123&user_name=Lefteris

This ensures your email links will always work and that you will open from Mobile Safari the links ONLY if your app has been installed on the device...

Finally, just a note, the URL scheme is appname:// and not http://appname

Now to explain the part 1 better, in our AppDelegate, we can do this in the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions delegate:

//if user has not set the app installed cookie, set it now.
bool hasAppInstalledCookie = [[NSUserDefaults standardUserDefaults] boolForKey:@"appInstalledCookie"];
if (!hasAppInstalledCookie) {
    //mark it was set
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"appInstalledCookie"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    //open the web browser
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.myApp.com/appInstalled"]];
}

Now in our appInsalled page, (index.html for example), we just set a cookie (any cookie name we want) then we kick the user back to our app, like this:

<script type="text/javascript">
    window.location = 'appName://';
</script>

The reason we are using a cookie, is to use this cookie, when the user opens up an email link. We will check if the browser is mobile safari AND the cookie is installed. This way, we know the user has installed our app, and the redirect will work properly.

Lefteris
  • 14,550
  • 2
  • 56
  • 95
  • @Lefteris: Thanks for the good answer. I didn't understood the Step-1 in Part-1 completely. Can you pls elaborate more? – nkongara Mar 25 '13 at 18:34
  • The idea is that we want to add a cookie to the mobile safari browser. This is to verify that the user has installed our app. I'm editing my question to explain more – Lefteris Mar 25 '13 at 18:44
  • it is a good answer, but I need not to open url, it will confuse users and not user friendly. Is there any other way to store cookie from safari? – jigneshbrahmkhatri Apr 09 '13 at 12:31
  • No there is no other way, but this is done very fast. It's like single sign on fom FB. It's a split second since the cookie page is only a few bytes only.... – Lefteris Apr 09 '13 at 17:47
  • Of course, you can skip the cookie settings and just redirect all those that have mobile safari to your app scheme. If they haven't installed your app they will see an error, but you can redirect them to the appstore then... The solution for this is [here](http://stackoverflow.com/a/1109200/312312) – Lefteris Apr 09 '13 at 17:50
  • @Lefteris how should I make call back url? because after opening url in safari, I have to redirect it to my app. Where should I write code for that? Please tell me. – Krunal Dec 19 '13 at 13:29
  • I have written how to redirect back. It's the JavaScript. You need to replace appName with the URL Scheme you did register your application in iOS... – Lefteris Dec 19 '13 at 14:24
  • @Lefteris Ok. I have set all the things as you said. My question is here what does "appInstalled" means? Is it like our html page? If so, when that page will be called? Means, after calling 'openURL', will it automatically be called "appInstalled"? – Krunal Dec 20 '13 at 12:22
  • wont this fail if the user deletes there cookies? The user default has no way of knowing if the cookie exists or not. – LightningStryk Jul 15 '15 at 14:52
4

The marked answer above is correct and works fine. However, in iOS 9, Apple introduced Universal Links, which allows you to associate a particular web domain or a web link (path) to your app. This makes it easier to redirect to your app if the app is installed or redirect to your web page if the app is not.

Note that you might still need to support Scheme URI (possibly with a fallback to a url links if the app is not installed like here) for devices that are still running older versions of iOS (prior to iOS 9).

I have documented my experience with Universal Links here if anybody is interested.

Community
  • 1
  • 1
Has AlTaiar
  • 4,052
  • 2
  • 36
  • 37
1

Something like http://applinks.org/ will be more scalable and does not require setting cookies or defaults to handle.

LightningStryk
  • 920
  • 8
  • 11