使用呼叫目录分机

时间:2018-05-18 13:20:09

标签: ios swift xcode9 callkit caller-id

我为CallKit扩展试了很多解决方案来显示来电显示。

步骤:

  1. 将调用目录扩展名添加到我的项目中。
  2. 设置developer.apple.com事物和应用程序组(供使用 userDefaults)
  3. 实施代码。 (用于测试,两个电话号码阵列都是 存储在userDefaults和静态号码中

  4. 在设备设置中允许我的应用进行呼叫识别和阻止

    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)
        }
    }
    
  5. 要测试的静态数字:

    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或其他地方添加任何代码?我的意思是应该在主应用程序中处理来电?你能救我吗?

0 个答案:

没有答案