Calling a phone number in swift

The Solution to Calling a phone number in swift is

Just try:

if let url = NSURL(string: "tel://\(busPhone)") where UIApplication.sharedApplication().canOpenURL(url) {

assuming that the phone number is in busPhone.

NSURL's init(string:) returns an Optional, so by using if let we make sure that url is a NSURL (and not a NSURL? as returned by the init).

For Swift 3:

if let url = URL(string: "tel://\(busPhone)"), UIApplication.shared.canOpenURL(url) {
    if #available(iOS 10, *) {
    } else {

We need to check whether we're on iOS 10 or later because:

'openURL' was deprecated in iOS 10.0

~ Answered on 2014-12-02 22:14:39

Most Viewed Questions: