Friday, November 5, 2010

Launching Your Own Application via a Custom URL Scheme

How to open other applications like phone dialer, SMS, Safari, Google Maps, iTunes or AppStore or any other iPhone application?

“openURL” is the API to use to achieve any of the above and many more.
Examples of some of the key applications that you can launch via URL are:
1. Launch the Browser
2. Launch Google Maps
3. Dial a Phone Number
4. Launch the SMS Application
5. Launch Apple Mail
6. Launch the AppStore

Launch the Browser
Here is a simple example of how to open Safari with a specific URL:
NSURL *url = [NSURL URLWithString:@""];
[[UIApplication sharedApplication] openURL:url];

Launch Google Maps
The URL string for launching Google Maps with a particular keyword follows this structure:${QUERY_STRING}

The only trick to this is to ensure that the value for the ${QUERY_STRING} is properly URL encoded. Here is a quick example of how you would launch Google Maps for a specific address:
// Create your query ...
NSString* searchQuery = @"1 Infinite Loop, Cupertino, CA 95014";

// Be careful to always URL encode things like spaces and other symbols that aren't URL friendly
searchQuery = [addressText stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

// Now create the URL string ...
NSString* urlString = [NSString stringWithFormat:@"", searchQuery];

// An the final magic ... openURL!
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];

Dial a Phone Number (available in iPhone only)
You can use openURL: to dial a phone number. One advantage this has over other URLs that launch applications, is that the dialer will return control back to the application when the user hits the “End Call” button.
The URL scheme for this is:

Here is an example of how we would dial the number (800) 846-6802:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://8008466802"]];

NOTE: Country code also needs to be including while dialing an international number

Launch the SMS Application (iPhone only)
It provides the ability to quickly setup the SMS client so that your users can quickly send a text message.

The format looks like this:

Here is an example which shows to send an SMS. Note that an SMS url doesn't use the "//" syntax.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:55555"]];

Launch Apple Mail
Also very useful, is the ability to enable a user to quickly send an email by launching the email client in compose mode and the address already filled out. The format of this URI should be familiar to anyone that has done any work with HTML and looks like this:


For example, here we are opening the email application and filling the “to:” address with :
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://"]];

Launching the AppStore
Initially, it is worth noting that you can launch the AppStore and have the "buy" page of a specific application appear. To do this, there is no special URL scheme. All you need to do is open up iTunes to the application you want to launch; right-click on the application icon at the top left of the page; and select Copy iTunes Store URL.

The URL will look something like this:

Launching the AppStore URL is exactly the same as you would launch the browser. Using the link above, here is an example of how we would launch the AppStore:
NSURL *appStoreUrl = [NSURL URLWithString:@""];
[[UIApplication sharedApplication] openURL:appStoreUrl];