CNContact Frame does not allow receiving all contacts (iCloud, Gmail)

Settings> Contacts> The default account is selected as iCloud and a new connection is saved. Then the default account as gmail changed. CNContactStore does not retrieve a new saved contact.

CNContactStore *store = [[CNContactStore alloc] init]; [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted == YES) { NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey,CNContactEmailAddressesKey]; NSArray * contactContainerArray = [store containersMatchingPredicate:nil error:nil]; for(CNContainer * container in contactContainerArray) { NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:container.identifier]; NSError *error; NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error]; } 
+5
source share
1 answer

If you just want to get all unified contacts (contacts in different accounts that represent the same person can be automatically connected to each other. Connected contacts appear in macOS and iOS applications as unified contacts), the best solution would be below one

  -(void)fetchContacts { CNContactStore *store = [[CNContactStore alloc] init]; [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted == YES) { //keys with fetching properties NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey,CNContactEmailAddressesKey]; NSString *containerId = store.defaultContainerIdentifier; NSArray * contactContainerArray = [store containersMatchingPredicate:nil error:nil]; CNContactFetchRequest * fetchRequest = [[CNContactFetchRequest alloc]initWithKeysToFetch:keys]; [store enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) { }]; } } 
+4
source

Source: https://habr.com/ru/post/1268208/


All Articles