Adding a URL Scheme to Your App

If you want your app to be supported by popular apps like Launch Center Pro, you need to add URL scheme support to your app. Plus, the new today extensions (widgets) in iOS 8 make URL schemes even more important. In order to launch your app from a widget, your app needs to support URL schemes.

Here’s how to do it (it’s easy).

Create a URL Scheme

In the project navigator on the left, select your project name, and select your target and select the Info tab. Expand the URL Types section, and click the + button.

Here, you only need to add two things: an Identifier, and a URL Scheme:

URLTypes

The Identifier should be a unique string. Apple recommends using a reverse-domain style name to ensure uniqueness. The URL Scheme is the thing that will actually launch your app. The url scheme you are probably most familiar with is http://—the scheme that precedes all URLs on the web.

In the example above, our app can now be launched with tinyURLApp://

To test it, download this sample app and install it on a simulator or device. Then, go to safari and type in tinyURLApp:// in the address bar and tap Go. Our sample app launches!

Doing More with URL Schemes

You can do a lot more with URL schemes, too. For example, you can take specific actions, or display a specific part of your UI, depending on the exact URL your app is launched with.

In your app, implement the following method in your App Delegate in order to handle being launched or brought to the foreground by a URL:

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

Type the following into Safari on your device or simulator with the sample app installed to see what happens: tinyURLApp://display/word?example

The app launches and displays the word “example” in the UI. Take a look at the code in the openURL method that makes it work:

if ( [[url host] isEqualToString:@"display"] 
          && [[url path] isEqualToString:@"/word"]) {
      self.viewController.label.text = [url query];
      return YES;
}

As you can see, it’s really easy to parse a URL. We can extract the scheme (tinyURLApp), the host (display), the path (/word) and the query (example). You can extract other information too, as detailed in Apple’s documentation.

Launching Your App From Code

If you want to launch your own app (or indeed, any other app) from code, you simply call this in the application:

NSURL *url = [NSURL URLWithString:@"tinyURLApp://display/word?example"];
[[UIApplication sharedApplication] openURL:url];

Of course, it’s a good idea to test if an app is installed first, which you can do with:

NSURL *url = [NSURL URLWithString:@"tinyURLApp://display/word?example"];
if ([[UIApplication sharedApplication] canOpenURL:url) {
     // Do something useful here
}

There’s a lot more information about integrating 1Password with your app at the Agile Blog.

Launching From An Extension in iOS8

A special case is launching from an extension in iOS 8—for example, if you want to launch your main app from a widget, you would use the extension context instead of the sharedApplication to open a URL:

[self.extensionContext openURL:url];

And that’s it. I’ve uploaded a small sample project here if you want to see all this in action or play with it. If you have any questions or problems, feel free to post a comment.

3 thoughts on Adding a URL Scheme to Your App

  1. I suppose that extensionContext is from the view controller itself, isn’t it?
    I made a mistake creating a NSExtensionContext object, causing the url failed to open.

  2. Pingback Open URL scheme from iOS extension | FYTRO SPORTS

Leave a Reply to Yee Ling Cancel reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>