Appendix B. Invoking External Applications

Your iPhone application can programmatically invoke other iPhone applications. Moreover, you can open your iPhone application to be invoked by other iPhone applications. To accomplish that, you specify a new URL scheme in the application's bundle, and the system will register that new scheme once the application is installed.

To invoke another iPhone application, you use the UIApplication instance method openURL: and pass in the URL of that application. The following code fragment will open the Maps application and display a specific address.

NSString  *address = @"http://maps.google.com/maps?q=plano,tx";
NSURL *myURL = [NSURL URLWithString:address];
[[UIApplication sharedApplication] openURL:myURL];

To register a new URL scheme, you need to add it to the Info.plist file. In Figure B.1, we show what needs to be added to register the new URL scheme lookup. The URL identifier can be any unique string.

Adding a new URL scheme in the Info.plist file.

Figure B.1. Adding a new URL scheme in the Info.plist file.

To actually service the invocation, you need to implement the application delegate's method application:handleOpenURL:, which is declared as follows:

- (BOOL)
      application:(UIApplication *)application handleOpenURL:(NSURL *)url

You receive an instance of NSURL encapsulating the URL used in the invocation. You can use the many methods of NSURL in order to retrieve queries, parameters, and fragments ...

Get iPhone SDK 3 Programming: Advanced Mobile Development for Apple iPhone and iPod touch now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.