How To Get Account Name From Contact Framework

▼魔方 西西 提交于 2019-12-02 20:02:46

问题


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:


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
                    }

                }
            }
        }
            }
        }];
}


来源:https://stackoverflow.com/questions/44133669/how-to-get-account-name-from-contact-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!