Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios url redirect from mail to app

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?

like image 666
Eugene Avatar asked Mar 25 '13 18:03

Eugene


People also ask

How do I redirect a URL to the app?

Once you build a link with same signature as mentioned in manifest The android system will add your app in chooser to open link in your app and you will get the data in "getIntent(). getData()" in respective Activity. If app is not installed the link will itself open in browser. Then handle it on browser .

How do I send a link to an app on my iphone?

On your iOS device, you can share an app directly from the App Store by finding and clicking on the application you want, then scrolling down to "Tell a Friend" which lets you send an email with a link to the application.


1 Answers

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.

like image 87
Lefteris Avatar answered Sep 28 '22 02:09

Lefteris