ios - CNContactViewController 与 CallKit 一起处理 CNContactPhoneNumbersKey
问题描述
我有一个使用 CallKit 集成的 VoIP 应用程序。
在我的联系人屏幕中,我有UITableView
所有设备联系人,当用户按下联系人时,我会填充CNContactViewController
:
extension ContactsViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedContact = viewModel.contactAt(section: indexPath.section, index: indexPath.row)
Logd(self.logTag, "\(#function) \(String(describing: selectedContact))")
let contactViewController = CNContactViewController(for: selectedContact!)
contactViewController.title = CNContactFormatter.string(from: selectedContact!,
style: CNContactFormatterStyle.fullName)
contactViewController.contactStore = viewModel.contactStore
contactViewController.allowsActions = false
contactViewController.delegate = self
navigationItem.titleView = nil
navigationController?.pushViewController(contactViewController, animated: true)
tableView.deselectRow(at: indexPath, animated: false)
}
}
这会毫无问题地填充联系人详细信息视图。
我想捕捉用户对电话号码按下的操作并执行 VoIP 呼叫,因此我使用以下代码:
extension ContactsViewController: CNContactViewControllerDelegate {
func contactViewController(_ viewController: CNContactViewController,
shouldPerformDefaultActionFor property: CNContactProperty) -> Bool {
if property.key == CNContactPhoneNumbersKey {
let phoneNumberProperty: CNPhoneNumber = property.value as! CNPhoneNumber
let phoneNumber = phoneNumberProperty.stringValue
makeMyVoIPCall(number: phoneNumber!, video: false)
//makeMyVoIPCall(number: "+1234567890", video: false)
return false
}
if property.key == CNContactSocialProfilesKey {
let profile: CNSocialProfile = property.value as! CNSocialProfile
if profile.service == appServiceName {
let phoneNumber = profile.username
makeMyVoIPCall(number: phoneNumber!, video: false)
return false
}
}
Logd(self.logTag, "\(#function) nothing to handle for \(property)")
return true
}
func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
dismiss(animated: true, completion: nil)
}
}
结果,当我按下电话项目元素启动 2 个电话时!一个呼叫来自我的应用程序 (VoIP),另一个来自系统 (SIM/GSM)。
我尝试了什么:
- 添加了上面的代码来处理
CNContactSocialProfilesKey
,在这种情况下,调用仅通过我的应用程序按预期执行一次。 - 将 makeMyVoIPCall 更改为特定数字,而不是按下(参见上面的注释行)。我再次看到 2 个调用系统调用 clicked 属性,而我的应用程序调用“+1234567890”。
- 我还验证了在处理操作时返回值应该是 false 而不是 true。
需要什么才能告诉系统我正在处理该操作,并且不应执行 SIM/GSM 呼叫?
我正在 iOS 12.1.1 (16C50) 上进行测试。
解决方案
将 包裹makeMyVoIPCall(number: phoneNumber!, video: false)
在主线程包中解决了这个问题。
我无法真正理解为什么 iOS 会忽略该false
值。
推荐阅读
- vue.js - VueJS 在 DOM 中渲染一些 v-binds(函数和数据)
- html - 如何使用工具从网站上抓取文本内容
- addeventlistener - 在关闭 Firefox 侧边栏时向内容脚本发送消息
- python - pymysql 执行 sql-sump
- html - 面板外的引导行
- python - 使用 Homebrew 升级 python 后出现“库未加载”错误
- machine-learning - 是否可以从 Kaggle 比赛中下载带有真实标签的测试数据?
- javascript - 我可以在执行脚本以将用户重定向到另一个页面时停止解析所有其他代码吗?
- django - 给定特定订单,从特定项目开始切片
- php - 如何在 PHP 中发送带有附件的邮件