Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 592
Plasma Avatar asked Aug 10 '19 12:08

Plasma


2 Answers

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.

like image 57
Petesh Avatar answered Sep 23 '22 11:09

Petesh


For me this seemed to be caused by the presence of CNContactNoteKey in the "keys to fetch". Removing it restores functionality.

I see you have CNContactNoteKey present, too.

I have flagged this to Apple as a bug.

like image 27
Peter Johnson Avatar answered Sep 21 '22 11:09

Peter Johnson