Having devoted much of my time over the last few months developing for the iPhone, I thought it was time to start collecting a small library of useful code snippets here on my blog. I’m mostly posting these for my own use, so that I don’t have to keep searching Google to keep finding the useful bits of code. However, I’ll try to make the posts as accessible as possible, with some explanation where necessary.

Launching Safari from within an iPhone App

The openURL method of UIApplication is a very useful and valuable method. It provides a quick and simple way to leave the current application and load up the web browser, the email application or even the phone application. It also provides a way for you to set the ‘default’ data, such as the start page in safari, or the email recipient for mail.

Using the function to launch Safari with a specific URL is simple, it requires barely any code. The following example shows how one would use openUrl to launch Safari having clicked a button.

-(IBAction)linkButtonClick:(id)sender {
NSString* launchUrl = @"http://www.johnwordsworth.com/";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]];
}

Launching Mail, the Phone or SMS

Launching the mail client, phone or the SMS client is equally as simple. The openURL method examines the first few characters of your URL and then uses this information to determine what information you have provided. It then does it’s best to enact what you have requested.

The following functions give you a quick and easy way to launch the relevant apps quickly and easily.

// Launch the email client with mailto://[recipient]
-(BOOL)launchMailWithRecipient:(NSString *)recipient {
NSString* launchUrl = [NSString stringWithFormat:@"mailto://%@", recipient];
return [[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]];
}
// Launch the phone with tel://[number]
-(BOOL)launchPhoneWithNumber:(NSString *)number {
NSString* launchUrl = [NSString stringWithFormat:@"tel://%@", number];
return [[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]];
}
// Launch the SMS client with sms:[number]
-(BOOL)launchSMSWithNumber:(NSString *)number {
NSString* launchUrl = [NSString stringWithFormat:@"sms:%@", number];
return [[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]];
}

But the iPod doesn’t have a phone!?

That’s right - there’s something we’ve not yet touched upon, and that’s the fact that openURL returns a boolean value. This value lets you know whether or not the operation completed successfully. Obviously, if it did - then you’d better clean up your App pretty quick as it’s not going to be in the foreground for much longer! However, more usefully, it allows you to handle the case when the supported operation is not permitted.

For instance, if you’re hoping to loading up the phone app on an iPod Touch, then it’s not going to work. In this case, you might want to popup a UIAlertView to let the user know that it’s not going to work. If you’re really working hard towards a friendly user interface, then you can use the canOpenUrl method to check this out without actually leaving the application. For instance, if canOpenUrl returns false, then you could disable the links or buttons that would otherwise launch the phone or SMS application.

How would you do that?

-(BOOL)doesDeviceHaveAPhone {
NSString* launchUrl = @"tel://0";
return [[UIApplication sharedApplication] canOpenUrl:[NSURL URLWithString: launchUrl]];
}

I’ll leave you to think about the other cunning things that you can do, such as launching with an http url that links directly to a google map / youtube video or custom Google search string. Anyhow, when combined with other elements of a productivity app, this can be quite useful! Enjoy.