我为CallKit扩展试了很多解决方案来显示来电显示。
步骤:
实施代码。 (用于测试,两个电话号码阵列都是 存储在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或其他地方添加任何代码?我的意思是应该在主应用程序中处理来电?你能救我吗?