iosobjective-cxcodecncontactcncontactstore

IOS 13 CNContacts No Longer Working To Retrieve All Contacts


I have an app that has worked perfectly well with the CNContacts framework all the way up to IOS 12. I'm currently testing it with IOS 13 beta and its completely broken. I've checked the contacts permissions and deleted the app and re-allowed the permissions. This is the code I'm using to retrieve all contacts:

NSError* error;

CNContactStore *store = [[CNContactStore alloc]init];

[store containersMatchingPredicate:[CNContainer predicateForContainersWithIdentifiers: @[store.defaultContainerIdentifier]] error:&error];

NSArray *keysToFetch =@[CNContactGivenNameKey, CNContactEmailAddressesKey, CNContactNoteKey];

CNContactFetchRequest *request = [[CNContactFetchRequest alloc]initWithKeysToFetch:keysToFetch];

BOOL success = [store enumerateContactsWithFetchRequest:request error:&error usingBlock:^(CNContact * __nonnull contact, BOOL * __nonnull stop){

       NSLog(@"Contact Found: %@", contact.givenName);

}];

The contact store builds fine, and the error is nil. However when I then try to get the contacts via the fetch request I get success=NO and I receive the following error:

Error Domain=CNErrorDomain Code=102 "(null)" UserInfo={CNKeyPaths=(
    note
), CNInvalidRecords=(
    "<CNContactFetchRequest: 0x60000189aa00: predicate=(null), keysToFetch=(\n    givenName,\n    emailAddresses,\n    note\n), unifyResults=1, sortOrder=0>"

I've tried various tweaks but I cannot get this to work at all. I also can't find any documentation to say this has behaviour has been changed.

Has anyone else also tried this or found a work around?


Solution

  • In iOS 13, apple have added a new entitlement that is needed if you wish to access the notes for contacts. The entitlement is com.apple.developer.contacts.notes. You can request permission to use this entitlement for an app being put in the App Store.

    The reason it was added is primarily for privacy reasons — the notes field can contain any information you might have on the contact; and a lot of times this information is significantly more sensitive than just the contact information.

    As of 2019-08-15, this entitlement sits in the beta category, which means it may be subject to change before the release of iOS 13, and the name of the entitlement will probably stabilize in it’s presentation to the developer in Xcode.