0

I've tried many solution for CallKit extension to show caller id.

Steps:

  1. Adding call directory extension to my project.
  2. Set the developer.apple.com things and app groups(for using userDefaults)
  3. Implement the code. (For testing, both phone numbers array which is stored in userDefaults and static number)

  4. Allow my app in device settings for call identification and blocking

    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)
        }
    }
    

For static numbers to test:

private func retrievePhoneNumbersToIdentifyAndLabels() -> (phoneNumbers: [String], labels: [String])? {
    // retrieve list of phone numbers to identify, and their labels
    return (["EXAMPLE_NUMBER"], ["EXAMPLE_NAME"])
}

For real life using:

    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)
}

But it didn't work. When I call myself from a number which is implemented in addAllIdentificationPhoneNumbers function, I can see only phone number and country name. Do I have to add any code in AppDelegate or somewhere else? I mean incoming call should be handled in main app? Can you help me?

Oliver
  • 599
  • 6
  • 14
  • Please edit the question to show your code. The extension needs to register the numbers and names. For testing your main app doesn't need to do anything. For a real solution your main app would download the data and make it available to your extension via an app group. – Paulw11 May 18 '18 at 13:46
  • I added my code. Can you please take a look @Paulw11 – Oliver May 18 '18 at 15:11
  • Are you sure that your phone numbers are parsing correctly to an Int64. The string version of the number can not contain any spaces or other formatting – Paulw11 May 18 '18 at 22:25
  • In for statement which is in addAllLIdenticationPhoneNumbers function, does CXCallDirectoryPhoneNumber(phoneNumber) convert string to necessary format? If it doesn’t, what can I do? @Paulw11 – Oliver May 18 '18 at 22:33
  • It does not. If you look at the declaration you `CXCallDirectoryPhoneNumber` you will see that it is just a typealias for `Int64`, so if the string cannot be converted to an Int64 it will just return `nil`. You need a plain string of numbers. So, for example the phone number "+1 (212) 555-4678" would need to be "12125554678" before it could be registered with the extension. Also you must pass numbers in ascending order – Paulw11 May 18 '18 at 22:37
  • I applied all of them but there is no change. Am I missing some significant part? – Oliver May 19 '18 at 01:51
  • You need to edit your question to show a [mcve] using your current code that demonstrates the issue – Paulw11 May 19 '18 at 01:53
  • Any solution on this? – ragul ml Dec 31 '19 at 05:56

0 Answers0