How To Get Account Name From Contact Framework

827 Views Asked by At

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

I mean, I need to differentiate Email and Phone synced contacts. Is there any way to do so?

I am using contact framework. I am getting identifier by using CNContainer, but how to get account name in which contacts are stored and also how fetch that contact’s from that account?

1

There are 1 best solutions below

0
On

Can you please try fetching All container's and then you can Group those 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
                    }

                }
            }
        }
            }
        }];
}