163

openURL has been deprecated in Swift 3.

Can anyone provide some examples of how the replacement openURL:options:completionHandler: works when trying to open an url?

pkamb
  • 33,281
  • 23
  • 160
  • 191
Shane O'Seasnain
  • 3,534
  • 5
  • 24
  • 31

5 Answers5

409

All you need is:

guard let url = URL(string: "http://www.google.com") else {
  return //be safe
}

if #available(iOS 10.0, *) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
    UIApplication.shared.openURL(url)
}
iDevAmit
  • 1,550
  • 2
  • 21
  • 33
Devran Cosmo Uenal
  • 6,055
  • 2
  • 26
  • 29
  • what if i use '+' operator in my url ? For example: "https://xxxxx.com./xxxxxxxxxxxxxx="+userName+"xxxxxxx="+userPass+"&xxxxxxxxx" like this. That string given me an error "No '+' candidates produce the expected contextual result type 'URL" – Ibrahim BOLAT Jan 05 '17 at 15:11
  • you have to use the + operator on your `String` instead on the `URL` – Devran Cosmo Uenal Jan 06 '17 at 11:06
  • Side Note: don't try to do this: UIApplication.shared.openURL(URL(string: "insert url here")!). The compiler on XCode 8 will get confused and not be able to build properly. So just use this solution as is. Works great! Thanks. – Joel Mar 21 '17 at 15:51
  • How would I open the url without actually opening Safari? How do I get the url to "open" in the background? Please answer my question at: http://stackoverflow.com/questions/43686252/swift-3-and-json-updating-the-database-by-running-a-url-in-the-background. – Christian Kreiter Apr 28 '17 at 18:29
  • 1
    You mean Swift doesn't make you climb walls to do something as complex as opening a URL? [jaw dropped] –  Dec 24 '18 at 03:17
  • it was killing me that options: [:] is required but options: nil won't compile – Tofu Warrior Dec 07 '20 at 03:26
44

Above answer is correct but if you want to check you canOpenUrl or not try like this.

let url = URL(string: "http://www.facebook.com")!
if UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
    //If you want handle the completion block than 
    UIApplication.shared.open(url, options: [:], completionHandler: { (success) in
         print("Open url : \(success)")
    })
}

Note: If you do not want to handle completion you can also write like this.

UIApplication.shared.open(url, options: [:])

No need to write completionHandler as it contains default value nil, check apple documentation for more detail.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
39

If you want to open inside the app itself instead of leaving the app you can import SafariServices and work it out.

import UIKit
import SafariServices

let url = URL(string: "https://www.google.com")
let vc = SFSafariViewController(url: url!)
present(vc, animated: true, completion: nil)
Chetan Rajagiri
  • 1,009
  • 11
  • 13
8

Swift 3 version

import UIKit

protocol PhoneCalling {
    func call(phoneNumber: String)
}

extension PhoneCalling {
    func call(phoneNumber: String) {
        let cleanNumber = phoneNumber.replacingOccurrences(of: " ", with: "").replacingOccurrences(of: "-", with: "")
        guard let number = URL(string: "telprompt://" + cleanNumber) else { return }

        UIApplication.shared.open(number, options: [:], completionHandler: nil)
    }
}
Senõr Ganso
  • 1,694
  • 16
  • 23
2

I'm using macOS Sierra (v10.12.1) Xcode v8.1 Swift 3.0.1 and here's what worked for me in ViewController.swift:

//
//  ViewController.swift
//  UIWebViewExample
//
//  Created by Scott Maretick on 1/2/17.
//  Copyright © 2017 Scott Maretick. All rights reserved.
//

import UIKit
import WebKit

class ViewController: UIViewController {

    //added this code
    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Your webView code goes here
        let url = URL(string: "https://www.google.com")
        if UIApplication.shared.canOpenURL(url!) {
            UIApplication.shared.open(url!, options: [:], completionHandler: nil)
            //If you want handle the completion block than
            UIApplication.shared.open(url!, options: [:], completionHandler: { (success) in
                print("Open url : \(success)")
            })
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


};
Ateş Danış
  • 162
  • 1
  • 3
  • 11