【问题标题】:What is wrong in my function to make a call我打电话的功能有什么问题
【发布时间】:2023-04-11 11:18:01
【问题描述】:

这是我拨打电话的代码

let phoneCall = phoneNumberTF.text
        if let phoneCall = phoneCall {
            let url = NSURL(string: "tel://\(phoneCall)")
            UIApplication.sharedApplication().openURL(url!)

        }else {
            let alert = UIAlertController(title: "Error", message: "not correct phone", preferredStyle: .Alert)
            let action = UIAlertAction(title: "Retry", style: .Default, handler: nil)
            alert.addAction(action)
        }

我在日志中收到此错误

ERROR: There is no registered handler for URL scheme tel

提示:我已经检查过这个问题,但解决方案对我不起作用 Calling a phone number in swift

【问题讨论】:

  • 你是在真机上测试吗?您无法在模拟器中拨打电话。
  • 顺便说一句 - 你应该检查openURL的结果。
  • @rmaddy 哦,我真的连打电话的控制器都打不开?
  • 错误消息来自对openURL的调用。这在模拟器上会失败,因为模拟器上没有手机。
  • 尝试将tel:// 更改为telprompt:// 而且它只适用于实际手机。

标签: ios swift


【解决方案1】:

根据 Apple 文档 https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/PhoneLinks/PhoneLinks.html

正确的格式是tel:。所以首先改变它,然后你应该在做之前检查UIApplication.sharedApplication.canOpenURL()。如果您在模拟器上运行,它将无法工作。

【讨论】:

  • 我可以安装应用程序在模拟器上拨打电话吗?
  • @MarcoDinatsoli 不,你需要在真实设备上运行它。
【解决方案2】:

正确的 url 方案类似于 tel:PHONE_NUMBER (不包括斜杠)。您还应该检查应用程序是否可以通过调用UIApplication.sharedApplication.canOpenURL()...打开您提供的网址...

let phoneNumber = "123-456-789"
let phoneURL = NSURL(string: "tel:\(phoneNumber)")!
if UIApplication.sharedApplication().canOpenURL(phoneURL) {
  UIApplication.sharedApplication().openURL(phoneURL)
}
猜你喜欢
  • 2018-02-19
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
  • 2015-05-27
  • 2018-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多