Open an iOS App from an Email link
Apple has allowed iOS apps to register their own URL schemes on your devices for a while now, but Iâ€™ve never used this functionality in-depth â€™till just recently.
Having your app register a URL scheme on a device means that you can open an installed application on a userâ€™s iPhone or iPad in ONE CLICK from their email (or the web, or another application).
We just used this for a software developmentÂ project where the client would send confirmation emails to the user after signup. The email would contain an link to a URL. The URL opens in mobile Safari, then launches the app and seamlessly logs the user into the app.
Thatâ€™s one use case; thereâ€™s probably others (let us know yours). Another one could be a URL embeded in textâ€”say someone is reading on their iPad and the content could prompt them to hop over to the app.
Try It Out
If a user has your app installed on their device, all you have to do is send them to my-great-app://whatever/ and Safari will resign to the background and your app will open. Why donâ€™t you try it? I know youâ€™ve got the Facebook app installed on your iPhone. Just clear your address bar and type: fb://profile
Go ahead, Iâ€™ll wait.
It opened the Facebook app and took you to your profile page (or gave you a big fat cryptic error message if you donâ€™t have the Facebook app installed). Facebook allows a LOT of options that you can pass to the application to take you into various parts of the app.
So enough about the Facebook app. What does a developer do if they want to pass a bunch of data to the appâ€”without actually defining what needs to be passed using a bunch of if/else or switch statements?
Itâ€™s fairly trivial to add url scheme to your applicationâ€™s Info.plist:
- Register the url scheme in your Info.plist. Lets say: my-great-app://
- In the application delegate, implement this method and return YES: [application:openURL:sourceApplication:annotation:](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html)
- Thatâ€™s it!
I wrote a method you can add to your application that allows the passing of data through the url with key/pair values like so:
When the user clicks a link like that, or you auto-redirect them from your site to that URL, you can parse the URL into an NSDictionary with this code. Hereâ€™s the method and an example implementation along with some pretty badass comments so you know what the heck is going on:
Update for Swift