Dale Dietrich
Dale Dietrich

Reputation: 7226

How to launch Safari and open URL from iOS app

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

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.

Upvotes: 227

Views: 250339

Answers (15)

Baher A
Baher A

Reputation: 127

Swift 5 updates to avoid deprecation in:

SFSafariViewController(url: url, entersReaderIfAvailable: true) // was deprecated in iOS 11.0

So we will have to use this initializer:

SFSafariViewController(url: URL, configuration: SFSafariViewController.Configuration)

like this:

if let url = URL(string: "http://www.yoururl.com/") {
    let config = SFSafariViewController.Configuration()
    config.entersReaderIfAvailable = true

    let vc = SFSafariViewController(url: url, configuration: config)
    present(vc, animated: true)
}

Swift 3 Solution with a Done button

Don't forget to import SafariServices

if let url = URL(string: "http://www.yoururl.com/") {
            let vc = SFSafariViewController(url: url, entersReaderIfAvailable: true)
            present(vc, animated: true)
        }

Upvotes: 12

Dale Dietrich
Dale Dietrich

Reputation: 7226

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)
}

Note that we do NOT need to escape string and/or address, like:

let myNormalString = "https://example.com";
let myEscapedString = myNormalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!

In fact, escaping may cause opening to fail.

Upvotes: 399

Zgpeace
Zgpeace

Reputation: 4467

Swift 5:

func open(scheme: String) {
   if let url = URL(string: scheme) {
      if #available(iOS 10, *) {
         UIApplication.shared.open(url, options: [:],
           completionHandler: {
               (success) in
                  print("Open \(scheme): \(success)")
           })
     } else {
         let success = UIApplication.shared.openURL(url)
         print("Open \(scheme): \(success)")
     }
   }
 }

Usage:

open(scheme: "http://www.bing.com")

Reference:

OpenURL in iOS10

Upvotes: 4

Chetan Prajapati
Chetan Prajapati

Reputation: 2297

Here one check is required that the url going to be open is able to open by device or simulator or not. Because some times (majority in simulator) i found it causes crashes.

Objective-C

NSURL *url = [NSURL URLWithString:@"some url"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
   [[UIApplication sharedApplication] openURL:url];
}

Swift 2.0

let url : NSURL = NSURL(string: "some url")!
if UIApplication.sharedApplication().canOpenURL(url) {
     UIApplication.sharedApplication().openURL(url)
}

Swift 4.2

guard let url = URL(string: "some url") else {
    return
}
if UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

Upvotes: 55

Diego Jiménez
Diego Jiménez

Reputation: 1526

Because this answer is deprecated since iOS 10.0, a better answer would be:

if #available(iOS 10.0, *) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}else{
    UIApplication.shared.openURL(url)
}

y en Objective-c

[[UIApplication sharedApplication] openURL:@"url string" options:@{} completionHandler:^(BOOL success) {
        if (success) {
            NSLog(@"Opened url");
        }
    }];

Upvotes: 5

Dustin Williams
Dustin Williams

Reputation: 3913

Swift Syntax:

UIApplication.sharedApplication().openURL(NSURL(string:"http://www.reddit.com/")!)

New Swift Syntax for iOS 9.3 and earlier

As of some new version of Swift (possibly swift 2?), UIApplication.sharedApplication() is now UIApplication.shared (making better use of computed properties I'm guessing). Additionally URL is no longer implicitly convertible to NSURL, must be explicitly converted with as!

UIApplication.sharedApplication.openURL(NSURL(string:"http://www.reddit.com/") as! URL)

New Swift Syntax as of iOS 10.0

The openURL method has been deprecated and replaced with a more versatile method which takes an options object and an asynchronous completion handler as of iOS 10.0

UIApplication.shared.open(NSURL(string:"http://www.reddit.com/")! as URL)

Upvotes: 172

Kevin ABRIOUX
Kevin ABRIOUX

Reputation: 17695

Swift 4 solution:

UIApplication.shared.open(NSURL(string:"http://yo.lo")! as URL, options: [String : Any](), completionHandler: nil)

Upvotes: -2

Greg T
Greg T

Reputation: 3404

Swift 3.0

if let url = URL(string: "https://www.reddit.com") {
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(url, options: [:])
    } else {
        UIApplication.shared.openURL(url)
    }
}

This supports devices running older versions of iOS as well

Upvotes: 11

Dilip Tiwari
Dilip Tiwari

Reputation: 1451

In Swift 1.2, try this:

let pth = "http://www.google.com"
    if let url = NSURL(string: pth){
        UIApplication.sharedApplication().openURL(url)

Upvotes: 0

Antzi
Antzi

Reputation: 13424

The non deprecated Objective-C version would be:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://apple.com"] options:@{} completionHandler:nil];

Upvotes: 17

Shubham JAin
Shubham JAin

Reputation: 613

Try this:

NSString *URL = @"xyz.com";
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:URL]])
{
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:URL]];
}

Upvotes: 1

Ryan H.
Ryan H.

Reputation: 2593

openURL(:) was deprecated in iOS 10.0, instead you should use the following instance method on UIApplication: open(:options:completionHandler:)

Example using Swift
This will open "https://apple.com" in Safari.

if let url = URL(string: "https://apple.com") {
    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }
}

https://developer.apple.com/reference/uikit/uiapplication/1648685-open

Upvotes: 3

OurangZeb Khan
OurangZeb Khan

Reputation: 1132

In SWIFT 3.0

               if let url = URL(string: "https://www.google.com") {
                 UIApplication.shared.open(url, options: [:])
               }

Upvotes: 2

meaning-matters
meaning-matters

Reputation: 22946

And, in case you're not sure if the supplied URL text has a scheme:

NSString* text = @"www.apple.com";
NSURL*    url  = [[NSURL alloc] initWithString:text];

if (url.scheme.length == 0)
{
    text = [@"http://" stringByAppendingString:text];
    url  = [[NSURL alloc] initWithString:text];
}

[[UIApplication sharedApplication] openURL:url];

Upvotes: 17

Tim
Tim

Reputation: 60110

Take a look at the -openURL: method on UIApplication. It should allow you to pass an NSURL instance to the system, which will determine what app to open it in and launch that application. (Keep in mind you'll probably want to check -canOpenURL: first, just in case the URL can't be handled by apps currently installed on the system - though this is likely not a problem for plain http:// links.)

Upvotes: 21

Related Questions