Episode #117

URL Schemes

10 minutes
Published on May 1, 2014

This video is only available to subscribers. Get access to this video and 584 others.

In this episode I cover how you can expose your app's functionality through URL schemes. Inter-app communication is something that iOS is somewhat lacking in, but URL schemes can enable some handy integration scenarios.

Episode Links

Register Your URL Scheme

The first step is to register the URL scheme in the project's Info.plist. You can modify this directly or use the project editor UI to add your scheme.

You can, of course, have more than one scheme registered.

Handling the action

Your application may or may not be running when you get a URL open action from another app. In your app delegate, implement the following method:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {

    // custom logic   

    // return YES if you handled it
    return YES;
}

Note that I mistakenly returned NO from this method in the recording. I've corrected this in the source code.

Invoking the URL Scheme from the source app

It's not point offering up functionality to the user if they don't have the requisite app installed. To this end, you should hide the functionality that isn't available to them like this:

NSURL *url = ...
if (![[UIApplication sharedApplication]) {
  // hide the feature
}

If you've verified that the URL can be opened, now we can simply call:

NSURL *url = ...
[[UIApplication sharedApplication] openURL:url];

And the phone will switch over to the other app.