iPhone Code Snippets – Launching Safari and Other Apps

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.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

What do you think?

2 Responses to “iPhone Code Snippets – Launching Safari and Other Apps”

  • [...] iPhone Code Snippets – Launching Safari and Other Apps [...]

    iPhone Coding – Sending In Application Email on April 21st, 2010 at 3:42 pm
  • Great write up. The iPhone is truly awesome and I don’t like being without it. This time last year I had jumped in a pool with my iPhone and it was dead. I had to wait 10 days before I was able to get a new one. The phone I had was a cheap go phone. I really love all the apps that can be downloaded to the iPhone. The best part about the iPhone to me is the ability to check emails on the go. Thanks for the information.

    Timothy Kellar on July 3rd, 2010 at 11:38 pm

Leave a Reply