URL Schemes on iOS

The URL scheme is a useful little feature in iOS that allows unrelated applications to communicate with each other in a controlled way. One application can use a custom URL scheme registered by another to pass control to it, supplying arguments as required.

Standard URL Schemes

Some of the standard iOS apps have URL schemes registered and these come in useful in all kinds of apps, especially business apps. Mail can be invoked with mailto://, Phone can dial numbers using the tel:// scheme and the Messages app can be opened for sending text messages with the sms:// scheme.

These schemes work equally well from HTML links in web views or from code. The code is as simple as this:

NSURL *url = [NSURL URLWithString:@"tel://0123456789"];
[[UIApplication sharedApplication] openURL:url];

There are standard view controllers in the MessageUI framework that give a little more control over the process of sending email and SMS text messages but these simple URLs do the job in many cases. The MessageUI framework and other aspects of iOS messaging are covered in Learning Tree’s Building iPhone® and iPad® Applications: Extended Features course.

Custom URL Schemes

Registering a custom URL scheme for your own applications is remarkably simple. All you have to do is create an entry in your application’s info.plist file. For example, consider a scheme that Learning Tree could use in a course brochure application:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>com.learningtree.CourseBrochure</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>course</string>
        </array>
    </dict>
</array>

The CFBundleURLName is a unique identifier for the scheme and the scheme “protocols” are listed as strings under CFBundleURLSchemes. These can be opened just like the standard schemes shown above.

NSURL *url = [NSURL URLWithString:@"course://2761"];
[[UIApplication sharedApplication] openURL:url];

The URL (including any parameters) is available to the target application in the application delegate:

- (BOOL)application:(UIApplication *)application
    openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication
    annotation:(id)annotation
{
    NSLog(@"%@", [url absoluteString]);
    return YES;
}

Obviously the usefulness of this technique is heavily dependent on other apps knowing about the scheme so that they can make use of it. Nevertheless, custom URL schemes would be a great way of integrating a suite of apps published in a corporate environment.

Do be careful though! Opening up an application with a custom URL scheme does have some security implications. You can learn more about issues like these on Learning Tree’s new Mobile Application and Device Security course.

Richard Senior

Type to search blog.learningtree.com

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.