ios - 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")
}
}
解决方案
如 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 时如何格式化电话号码。
推荐阅读
- javascript - React-native 如何使用 x 和 y 值将视图固定在某个位置
- python - 有没有办法在 Anaconda/Spyder 中更改配置文件的目录?
- android - 从android中的其他活动选定列表项中获取和存储数据在回收站视图中
- apache-spark - 将我的分析项目从 log4j 1.x 迁移到 log4j 2.x 并观察到第三方依赖项为 log4j 1.x
- python - 使用 python 和 Counters 计算一个单词在文本中出现的次数
- python - 在维护索引信息的同时迭代过滤的 Numpy 数组
- grafana - 使用 influx config 将空手道加特林数据写入监听不同端口的不同数据库
- angular - 以 Kendo UI 作为其依赖项发布 NPM 包的正确方法是什么?
- rdf - 如何绘制 RDF 设计图
- php - 使用 Drupal 7 更新数据库中是否已存在