openURL has been deprecated in Swift3. Can anyone provide some examples of how the replacement openURL:options:completionHandler: works when trying to open an url?
Asked
Active
Viewed 1.3e+01k times
156
Anbu.Karthik
- 80,161
- 21
- 166
- 138
Shane O'Seasnain
- 3,344
- 4
- 20
- 30
7 Answers
402
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,452
- 2
- 21
- 33
Devran Cosmo Uenal
- 5,943
- 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
-
1You mean Swift doesn't make you climb walls to do something as complex as opening a URL? [jaw dropped] – Daniel Springer 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
39
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
- 68,014
- 12
- 152
- 178
30
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
- 889
- 10
- 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,574
- 13
- 21
4
import UIKit
import SafariServices
let url = URL(string: "https://sprotechs.com")
let vc = SFSafariViewController(url: url!)
present(vc, animated: true, completion: nil)
Miroslav Glamuzina
- 4,374
- 2
- 17
- 33
Salman Khan
- 138
- 5
-
let vc = SFSafariViewController(url: url!) this will crash if url is nill, which is nil many times even after hardcoded string value. – Tabish Sohail Jun 11 '21 at 05:25
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ış
- 164
- 1
- 3
- 10
Scott Maretick
- 25
- 5
0
This works fine and doesn't leave the application.
if let url = URL(string: "https://www.stackoverflow.com") {
let vc = SFSafariViewController(url: url)
self.present(vc, animated: true, completion: nil)
}
Sk.Azad
- 1,880
- 2
- 15
- 19