Launch Safari with UIApplication openURL

John WordsworthiOS Development5 Comments

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.

John WordsworthLaunch Safari with UIApplication openURL

5 Comments on “Launch Safari with UIApplication openURL”

  1. Pingback: iPhone Coding – Sending In Application Email

    1. John Wordsworth

      Thanks for the spot, sometimes these things get mangled by the post editor when I make a minor tweak somewhere and I miss them.

  2. Mark

    Greetings All -

    I’m interested in finding a method to launch a URL in Safari WITHOUT losing control of my application. Specifically, I would like to launch the browser while still maintaining control of my application, which “openURL” doesn’t really allow. Any thoughts?

    Thank you in advance….

    - Mark.

    1. John Wordsworth

      Hi Mark,

      Unfortunately, there is no simple ‘one-liner’ solution to launching a full screen browser without losing control of your app.

      Fortunately, the solution isn’t that much work. You simply need to make your own ViewController with an embedded UIWebView control. There may already be a handily packaged solution out there on the web, but by building your own you can essentially create your own ‘In-App Mini Browser’ in a similar way to the Facebook app.

      Essentially, you just need to have a UIViewController with a UIWebView and a ‘close’ button. Adding a ‘back’ button is trivial – just drop a UIButton onto the form and hook it into the [mWebView goBack] method. If you want the user to be able to type in their own URLs instead of just navigation around the site you present them with, then that’s a little bit more work – but not much. It’s just a case of passing the URL to the web view when the user returns from the text field.

      Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *