如何在Swift中使用openURL打电话



我已经将代码从Objective-C转换为Swift,但是在Objective-C中,我们可以像这样设置我们想要打开的URL的类型(例如电话,短信,web):

@"tel:xx"
@"mailto:info@example.es"
@"http://stackoverflow.com"
@"sms:768number"

Swift中的代码是:

UIApplication.sharedApplication().openURL(NSURL(string : "9809088798")

我读到tel:没有发布任何方案参数,但是我不知道Swift是否可以检测到这个字符串是用于打电话,发送电子邮件,还是打开网站。或者我可以这样写:

(string : "tel//:9809088798")

?

我很确定你想:

UIApplication.sharedApplication().openURL(NSURL(string: "tel://9809088798")!)

(注意,在你的问题文本中,你写的是tel//:,而不是tel://)。

NSURL的string init需要一个格式良好的URL。它不会把一堆数字变成一个电话号码。你有时会在UIWebView中看到电话号码检测,但那是在比NSURL更高的层次上完成的。

编辑:新增!

Swift:

private func callNumber(phoneNumber:String) {
  if let phoneCallURL:NSURL = NSURL(string:"tel://(phoneNumber)") {
    let application:UIApplication = UIApplication.sharedApplication()
    if (application.canOpenURL(phoneCallURL)) {
      application.openURL(phoneCallURL);
    }
  }
}

现在,您应该能够使用callNumber("7178881234")拨打电话。

对于iOS中的Swift:

var url:NSURL? = NSURL(string: "tel://9809088798")
UIApplication.sharedApplication().openURL(url!)

您需要记住删除空格,否则它将不起作用:

if let telephoneURL = NSURL(string: "telprompt://(phoneNumber.stringByReplacingOccurrencesOfString(" ", withString: ""))") {
        UIApplication.sharedApplication().openURL(telelphoneURL)
    }

"telprompt://"将提示用户呼叫或取消,而"tel://"将直接呼叫。

@ profile:

问题是,你的解决方案不会返回到应用程序之后在iOS7上完成通话。——6月19日13:50

, @ Zorayr

嗯,很好奇是否有解决方案可以做到这一点…可能是

使用

UIApplication.sharedApplication().openURL(NSURL(string: "telprompt://9809088798")!)

你将得到一个Call/Cancel的提示,但是它返回到你的应用程序。恐怕没有办法返回(没有提示)

你必须插入"+"是另一种方式

private func callNumber(phoneNumber:String) {
  if let phoneCallURL:NSURL = NSURL(string:"tel://"+"(phoneNumber)") {
    let application:UIApplication = UIApplication.sharedApplication()
    if (application.canOpenURL(phoneCallURL)) {
      application.openURL(phoneCallURL);
    }
  }
}

Swift 3小更新

UIApplication.shared.openURL(NSURL(string: "telprompt://9809088798")! as URL)

下面的代码片段可以判断SIM卡是否存在以及设备是否能够进行呼叫,如果ok则会进行呼叫

  var info = CTTelephonyNetworkInfo()
    var carrier = info.subscriberCellularProvider
    if carrier != nil && carrier.mobileNetworkCode == nil || carrier.mobileNetworkCode.isEqual("") {
       //SIM is not there in the phone
    }
    else if UIApplication.sharedApplication().canopenURL(NSURL(string: "tel://9809088798")!)
    {
    UIApplication.sharedApplication().openURL(NSURL(string: "tel://9809088798")!)
    }
    else    
    {
      //Device does not have call making capability  
    }

在swift 5.1中调用,只需使用以下代码:(我已经在Xcode 11中测试过了)

let phone = "1234567890"
if let callUrl = URL(string: "tel://(phone)"), UIApplication.shared.canOpenURL(callUrl) {
     UIApplication.shared.open(callUrl)
}

编辑:对于Xcode 12.4, swift 5.3,只需使用以下命令:

UIApplication.shared.open(NSURL(string: "tel://555-123-1234")! as URL)

确保你导入了UIKit,否则它会说它在作用域中找不到UIApplication

For swift 3

if let phoneCallURL:URL = URL(string:"tel://(phoneNumber ?? "")") {
            let application:UIApplication = UIApplication.shared
            if (application.canOpenURL(phoneCallURL)) {
                application.open(phoneCallURL, options: [:], completionHandler: nil);
            }
        }

对于swift 4:

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

然后使用函数:

let phoneNumber = "tel://+132342424"
call(phoneNumber: phoneNumber)

迅速4及以上

let dialer = URL(string: "tel://5028493750")
    if let dialerURL = dialer {
        UIApplication.shared.open(dialerURL)
}

我更喜欢将URL的创建推迟到内置的,如:

var components = URLComponents()
components.scheme = "tel"
components.path = "1234567890"
print(components.url!) //Prints out: "tel:1234567890"

可以在UIApplication.shared.openURL

中使用

最新更新