ios url redirect from mail to app
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:
- When your app is run for the First time, open from withing your app the Safari browser and send it to your website.
- In the website, install a cookie for Safari (like myAppIsInstalled)
- 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:
- In your website you check if your app is installed (the cookie is present)
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.
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.