Launch Safari with UIApplication openURL

John WordsworthiOS Development20 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 = @"";
[[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

20 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.

      1. Kaylee

        It would seem so. If you look at the button commands at the bottom of the screen you will see that they are indeed from a 360 controler. Still, this topic is doing nothing causing but massive flame wars. I can’t wait till LoT does the final H2H and puts all this to rest.VN:F [19]117_.161.(from 0 votes)


        Pengalamanku kalau aku makan telur, makanan yg digoreng, cokelat, susu maka GERD-ku kambuh berat..kepala nyut-nyut karena asam lambung berlebih dan naik ke kerongkongan, akibat selama 2- 3 jam perutku seperti dapur gunung berapi…bersendawa (maaf) terus menerus….ampuun dah..


        Awesome! Can lesbians who pushed babies out of their own loins AND live in two-parent households where both parents work also have a district? Also, I don’t think the pact with Peeta and Katniss would work for people whose presumptive sexual orientation is out of alignment with one another. You and Joanne might have to duke it out for who is Katniss and who is Rue.


        The new channels are almost unusable, because we can't even open pages in a new window/tab. Clicking back a page doesn't even work most of the time, so then I have to reload to get back to the main channel page.My playlist descriptions don't show up when I view my list of playlists. I have to click them one at a time to see the descriptions. Since my channel is entirely devoted to playlists, my channel is essentially destroyed.The new channels are also very slow compared to the old channels (my internet speed is 11.0 Mbps, which is more than enough for most websites).

      5. cheap car insurance

        those pretzels at the mall get me every time! haha. we have something similar, i think, which we call BLOW money. unfortunately, this month we’ve already spent it – at the Iowa State Fair.


        What a great idea! But since my boys are older (read all teen/preteen…) I think we will use an old map book. We even have one of Europe! Thanks for the great idea… Now to actually get started on it. In fact this sounds like a great first sewing project for our eldest as our middlest had to make something for class last year.!!!


        You really think the game looks bad why do you say that. Crytek has also been at the cutting edge of graphics tech I have no doubt that on pc this game visually will be EPIC consoles should be good too if crysis 2 is any indication. Also keep in mind there is still dev time to tighten things up. I would love it if they upped the gore levels in the game to realistic levels. I want to see the horrors of combat and the carnage I have caused LOLVN:F [1.9.17_1161](from 5 votes)

    2. Delphia

      Wow, what a gorgeous day! It sounds like you had a wonderful time. Glad you got to see your family as well. I agree that the ring shot is bealuifut!

    3. buy cialis on line

      Gracia Monica por tu testimonio. Poderle poner nombre y apellido a lo que uno esta padeciendo es ya un gran avance. Por lo menos saber quien es el enemigo y así poder dominarlo. Me alegra te encuentres mejor. gracias de nuevo por compartir tu caso. Te deseo lo mejor. Cuidate. Maria L


      Hi everyone…I am srinivas working at VIRTUSA from the last 3 years.At the time when I completed my engineering the opportunities are less and the people are more to grab oppurtunity.At that time I got a very good support from TechGenius which showed me a correct path where I can prove myself and helped me to settle at a very good position in my life.I am very glad to share my experiences with TechGenius.I also appreciate the thought of creating a blog to share their views.Keep Rocking always as before and create a amazing world of knowledge.

    5. car insurance

      Les mots me manquent… Tout l’amour et le soutien du monde n’arriveront pas à panser cette blessure, si profonde. Toi, ton sourire et tes yeux resteront à jamais gravés dans nos coeurs. Une pensée particulière à tes parents et proches, à qui j’apporte mon soutien et ma tendresse.Rest in peace

    6. vordruck kündigung kreditkarte

      I LOVE realistic YA. It’s my favorite genre to read … and to write! Courtney Summers is one of my fave realistic fiction writers, along with Sarah Dessen, Jay Asher, Megan McCafferty, Sara Zarr, and Maureen Johnson. I’m definitely dying to read Amplified!

    7. http://www./

      I also had: Exception in thread “main� java.lang.NoClassDefFoundError: org/apache/commons/digester/SetNestedPropertiesRule.I was using jasperreports-1.1.1.jar but I reverted back to jasperreports-0.6.8.jar which seems to have solved this problem.

  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 *