3

I create numbers array from CNContact in singleton. But when I reload my CallKit extensions CallKit doesn't block number that I blocked before. Number length is 11 characters. Array isn't null. After reload CallKit Extension there is no error.

static let shared = BlockNumbersManager()
    private init() { }
    open var blockNumbers: [CXCallDirectoryPhoneNumber] = []
open func getIntegerBlockNumbers() -> [CXCallDirectoryPhoneNumber] {
        return blockNumbers
    }



private func addBlockingPhoneNumbers(to context: CXCallDirectoryExtensionContext) throws {
        let phoneNumbers: [CXCallDirectoryPhoneNumber] = BlockNumbersManager.shared.getIntegerBlockNumbers() // TODO: add numbers for block dynamically.
        for phoneNumber in phoneNumbers {
            context.addBlockingEntry(withNextSequentialPhoneNumber: phoneNumber)
        }
    }

What have I missed?

Paulw11
  • 108,386
  • 14
  • 159
  • 186

4 Answers4

2

While @Stuart 's answer makes quite good points, I'd like to add some tips from my side, after I spent over 1 hour to figure out why my Call Directory extension did not work...

  1. The phone number format: check if you've added the country code. In my case I tested with Australian mobile number like 0412345678, so it should be either 61412345678, or +61412345678. Yes both worked for me.

  2. Mind the iOS cache! What you've written in your Call Directory extension does not get executed/called each time you restart or run your app. i.e. You think you've appended some new phone numbers in the blocking array but iOS actually may not load it as you expected. The trick is to toggle your app's option in Settings -> Phone -> Call Blocking & Identification.

  3. You may want to call CXCallDirectoryManager.reloadExtension in your main app when a reload is needed to invalidate the iOS cache.

Wei WANG
  • 1,748
  • 19
  • 23
1

Currently in iOS 10.x, every time your Call Directory extension runs it must provide a complete set of phone numbers to either block or identify with a label. In other words, if it runs once and adds blocked phone numbers [A, B, C], and you wish to also block phone number D, then the next time the extension is run it must provide the complete list [A, B, C, D].

In iOS 11, some new APIs have been added which allow the extension to provide data incrementally whenever possible. For instance, if the extension has already run once and added blocked phone numbers [A, B, C], and you wish to also block phone number D, then the next time the extension is run it may first check if the system allows incremental loading, and if it does then it may add blocked numbers [D] and the full list of blocked numbers will become [A, B, C, D].

Note that when using the new iOS 11 incremental loading APIs, your extension must always check whether incremental loading is currently allowed. The system may occasionally need to reload the complete list of phone numbers and your extension must remain able to provide the complete list.

See the documentation for these new APIs at https://developer.apple.com/documentation/callkit/cxcalldirectoryextensioncontext

In addition, you may view the 'Call Directory Extension' iOS extension template in Xcode 9 which has been updated to demonstrate the new incremental loading APIs:

Xcode Call Directory Extension target template screenshot

Stuart M
  • 11,458
  • 6
  • 45
  • 59
  • 2
    Hi. It's useful information but my problem is that when I hardcode my number like [3809...] than call kit block it but when I return number for CNContact than it doesn't block. Besides array that I return isn't nil. Every time I reload extension with full stack of numbers. – Severyn Katolyk Aug 28 '17 at 05:09
  • Are you saying that if you include a phone number that you retrieve from a CNContact instance in your list of phone numbers provided to the CallKit API, then that phone number is not blocked? If so, is that phone number a fully-qualified, international number, including the country code? Be sure to still include the country code, even when retrieving phone numbers from contact objects. – Stuart M Aug 29 '17 at 05:47
  • Yes You understand correctly and yes number is full. 12 digits in number. Fro example 38-093-444-44-44. – Severyn Katolyk Aug 29 '17 at 09:34
  • 1
    I think you'll need to either post a more complete code example to determine what the issue is, or file a bug with Apple's [Bug Report](https://bugreport.apple.com) (Radar) system detailing the issue and including your example code there. – Stuart M Aug 31 '17 at 05:48
1

Check each phone number in your contact list, if any number without country code are listed then it will not added to blocking list, you must have to provide Country code + Number

dilip
  • 163
  • 8
1

while parsing an array to blocking function you should note 5 points

  1. Check that Extension is enabled in settings (give permission to your app extention)
  2. must include country code
  3. Array must be in ascending order. while you sort your array it should be in integer or any numeric form (do not sort array in String form)
  4. In Array there should not be any duplicate numbers
  5. there should be proper numbers ( you should not block 123 like this do not wrong number )