我已经为 CallKit 扩展尝试了许多解决方案来显示来电显示。
脚步:
- 将呼叫目录扩展添加到我的项目。
- 设置 developer.apple.com 事物和应用组(用于使用 userDefaults)
实现代码。(为了测试,存储在 userDefaults 中的电话号码数组和静态号码)
在设备设置中允许我的应用进行呼叫识别和阻止
private func addAllIdentificationPhoneNumbers(to context: CXCallDirectoryExtensionContext) { guard let (phoneNumbersToIdentify, phoneNumberIdentificationLabels) = retrievePhoneNumbersToIdentifyAndLabels() else { NSLog("Unable to retrieve phone numbers to identify and their labels") let error = NSError(domain: "CallDirectoryHandler", code: 2, userInfo: nil) context.cancelRequest(withError: error) return } for (phoneNumber, label) in zip(phoneNumbersToIdentify, phoneNumberIdentificationLabels) { context.addIdentificationEntry(withNextSequentialPhoneNumber: CXCallDirectoryPhoneNumber(phoneNumber)!, label: label) } }
对于要测试的静态数字:
private func retrievePhoneNumbersToIdentifyAndLabels() -> (phoneNumbers: [String], labels: [String])? {
// retrieve list of phone numbers to identify, and their labels
return (["EXAMPLE_NUMBER"], ["EXAMPLE_NAME"])
}
在现实生活中使用:
private func retrievePhoneNumbersToIdentifyAndLabels() -> (phoneNumbers: [String], labels: [String])? {
// retrieve list of phone numbers to identify, and their labels
var numbers = [String]()
var names = [String]()
let sharedDefaults = UserDefaults.init(suiteName: "group.EXTENSION_NAME")
numbers = sharedDefaults?.array(forKey: "contactsPhoneNumbers") as! [String]
names = sharedDefaults?.array(forKey: "contactsNames") as! [String]
return (numbers, names)
}
但它没有用。当我从addAllIdentificationPhoneNumbers函数中实现的号码给自己打电话时,我只能看到电话号码和国家名称。我是否必须在 AppDelegate 或其他地方添加任何代码?我的意思是来电应该在主应用程序中处理?你能帮助我吗?