Launching your iOS application from another application using URL Scheme

Using Custom URL scheme for your iOS application

Why do I need this?
You need this if you want to launch your iOS application from a webpage, an Ad or even from another application.
Remember how the mail client open up when you click on a link like this: (mailto:someone@somewhere.com)? This is exactly how that happens.

To explain how it is done, you need to know what a URL scheme is.

URL Scheme:
Any url begins with a scheme name followed by a colon (:) the path and any queries in that order.

foo://something
here, “foo” is the scheme name.

mailto:username@example.com?subject=Topic
and here, “mailto” is the scheme name. followed by path “username@example.com” and query “subject=Topic”

For more details: URI Scheme – Wikipedia

Redirecting to phone/messages app from your application.
Dialling a number: tel:<phone number>

Sending an SMS to a number: sms:<phone number>?body=<messageText>

Adding custom URL scheme to your app:
1. Open the application Info.plist
2. Add a new row (right click -> Add New row OR click on + button on the first item: Information Property List)
3. Select “URL types” for key as below.

URLScheme14. Under “Item 0”, add another item by clicking + on URL types.
5. Select “URL Schemes” as key as below.

URLScheme26. Enter a value for “URL Identifier” say “com.<companyName>.<appName>”
7. Enter a value for “URL Scheme” say “<bundleName>”
8. The plist should look like the below screenshot.

URLScheme3

Testing:
1. Build and Run the application.
2. Click on home button and open Safari app.
3. Enter the URL scheme that you provided for your application in step 7 above. (say <appName>://)
4. It should open your application from the background.

safari

Hurray! 🙂

So what’s next?
What if you want to open a specific view in your application?
What if you wan to pass some data to your application through the URL?
For these, you will need to implement the below delegate method in your application’s App delegate.

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
#warning DELETE METHOD: Use application:openURL:sourceApplication:annotation: instead
    // NOTE: This method is marked for deprecation. so use the below instead.
    return YES;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    // Handle the action for URL here:
    NSLog(@"URL: %@\nSource Application: %@", url, sourceApplication);
    return YES;
}

Method Parameters:
methodparams

References:

  1. Apple URL Scheme Reference
  2. Apple App Programming guide: Inter-App Communication: URL Scheme
  3. UIApplicationDelegateProtocol Reference
  4. Apple Secure coding guidelines for using URL Schemes
Advertisements

One thought on “Launching your iOS application from another application using URL Scheme

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s