How to get account name from contact structure

We know that iOS contacts can be synced with Google , iCloud and Phone . Well, we can get a bunch of contacts using Contacts.framework , but I want to know which account it belongs to.

I mean, I need to distinguish between email and phone contacts. Is there any way to do this?

I am using contact framework . I get the identifier using CNContainer , but how do I get the name of the account in which the contacts are stored, and also how to get the contacts from this account?

0
source share
1 answer

You can try fetching the entire container, and then you can group these contacts according to the container name

  -(void)fetchContacts { CNContactStore *store = [[CNContactStore alloc] init]; [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted == YES) { NSArray * contactContainerArray = [store containersMatchingPredicate:nil error:nil]; for(CNContainer * container in contactContainerArray) { NSLog(@"Container name == %@ ",container.name); NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:container.identifier]; NSError *error; NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error]; if (error) { NSLog(@"error fetching contacts %@", error); } else { for (CNContact *contact in cnContacts) { NSMutableArray *contactNumbersArray = [[NSMutableArray alloc]init]; // contact.givenName; // contact.familyName; for (CNLabeledValue *label in contact.phoneNumbers) { // [label.value stringValue]; Phone Number } } } } } }]; } 
0
source

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


All Articles