首页 > 解决方案 > tel:// 方案自动格式化电话号码

问题描述

我试图用这个示例号码 639450200901 拨打电话,但似乎 iOS sdk 会自动格式化电话号码。在警报上显示这样一个“+63 945 020 0901”。有什么建议不要自动格式化,我想在没有这个 + 号的情况下继续通话吗?这是我的示例代码:

if let phoneNum = _params["recipient"] as? String,
           let percentEncodedString = phoneNum.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed),
           let url = URL(string: "tel:\(percentEncodedString)") {
        
            if (UIApplication.shared.canOpenURL(url)) {
                   UIApplication.shared.open(phoneNumUrl, options: [:], completionHandler: { ( completed ) in
                      print("Call completed")
            }
    }

标签: iosswift

解决方案


如 10.3 发行说明中所述。

https://developer.apple.com/library/content/releasenotes/General/RN-iOSSDK-10.3/

打开网址

当第三方应用程序在 tel://、facetime:// 或 facetime-audio:// URL 上调用 openURL:时,iOS 会显示提示并要求用户在拨号前确认。

因此,当您使用方案 tel:// 调用 URL 时,phoneNumber 将由操作系统格式化,并显示在提示符中。这是一个系统提示,我们对此无能为力。

但是,您可以在显示系统提示之前创建自定义警报并以所需格式显示 phoneNumber:

if let phoneNumber = phoneNumber, let phoneURL = NSURL(string: ("tel://" + phoneNumber)) {

    let alert = UIAlertController(title: ("Call " + phoneNumber + "?"), message: nil, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "Call", style: .default, handler: { (action) in
        UIApplication.shared.open(phoneURL as URL, options: [:], completionHandler: nil)
    }))

    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    self.present(alert, animated: true, completion: nil)
}

有关电话链接的更多信息,请查看:Apple URL Scheme Reference

您可以在此处RFC 2806中找到有关电话 URL 的更多详细信息,以及移动操作系统在解析电话 URL 时如何格式化电话号码。


推荐阅读