How to launch Safari and open URL from iOS app

Dale Dietrich picture Dale Dietrich · Sep 14, 2012 · Viewed 222.6k times · Source

On the settings page, I want to include three links to

  • My app support site
  • YouTube app tutorial
  • My primary site (ie: linked to a 'Created by Dale Dietrich' label.)

I've searched this site and the web and my documentation and I've found nothing that is obvious.

NOTE: I don't want to open web pages within my app. I just want to send the link to Safari and that link be open there. I've seen a number of apps doing the same thing in their Settings page, so it must be possible.

Answer

Dale Dietrich picture Dale Dietrich · Sep 14, 2012

Here's what I did:

  1. I created an IBAction in the header .h files as follows:

    - (IBAction)openDaleDietrichDotCom:(id)sender;
    
  2. I added a UIButton on the Settings page containing the text that I want to link to.

  3. I connected the button to IBAction in File Owner appropriately.

  4. Then implement the following:

Objective-C

- (IBAction)openDaleDietrichDotCom:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.daledietrich.com"]];
}

Swift

(IBAction in viewController, rather than header file)

if let link = URL(string: "https://yoursite.com") {
  UIApplication.shared.open(link)
}