UserDefaults in Call Directory Extension's Caller ID

772 Views Asked by At

This is the story:

In main app: - Some numbers are fetched from my web-service. These numbers append to NSUserDefaults.

In Call directory Extension:

  • Retrieving from UserDefaults:

    private func retrievePhoneNumbersToIdentifyAndLabels() -> (phoneNumbers: [CXCallDirectoryPhoneNumber], labels: [String]) {
    
        let defaults = UserDefaults.init(suiteName: GROUP_NAME)
    
        let phones = defaults?.array(forKey: "phoneArray") as! [CXCallDirectoryPhoneNumber]
        let names = defaults?.array(forKey: "nameArray") as! [String]
    
        return (phones,names)
        } }
    

And here is the Call Directory Extension's function:

private func addAllIdentificationPhoneNumbers(to context: CXCallDirectoryExtensionContext) {

    let (phoneNumbers, labels) = retrievePhoneNumbersToIdentifyAndLabels()

       print(phoneNumbers)
       print(labels)

    for (phoneNumber, label) in zip(phoneNumbers, labels) {
        context.addIdentificationEntry(withNextSequentialPhoneNumber: phoneNumber, label: label)
    }
}

Problem: When I am trying to enable Call Blocking & Identification in Setting on my device, it gives "Error Enabling Extension" error. And also, in debugger of Xcode,

[User Defaults] Couldn't read values in CFPrefsPlistSource<0x10fe251d0> (Domain: GROUP_NAME, User: kCFPreferencesAnyUser, ByHost: Yes, Container: (null), Contents Need Refresh: Yes): Using kCFPreferencesAnyUser with a container is only allowed for System Containers, detaching from cfprefsd

But here is the interesting parts of the story:

After these problems, somehow, print functions which in above function are running correctly. I mean, it can save phone numbers and labels to UserDefaults and also it can read from UserDefaults. To be clarify, I can reach UserDefaults in my extension. There is no problem.

Note: Of course, app groups of both main app and Call Directory Extension have been set and phone numbers have correct typealias which is Int64. And also, phone numbers are sorted ascending.

2

There are 2 best solutions below

0
On

Make sure that you don't accidentally corrupt the data. Sharing data files means there might be more than one process trying to use a file at the same time. Sandboxing on iOS means this is a somewhat rare situation, but that doesn't mean it's OK to ignore it. You may need to use "NSFileCoordinator"

you can find more explanatory information in this address : enter link description here

0
On

I had the same problem. You must test it in Ad-Hoc or Release mode! It seems that in Debug mode it doesn't work sometime!