7

I am using Contacts framework in an app. the thing I'm trying to do is categorize all the contacts by their containers. e.g. facebook contacts will be under the title "Facebook", google contacts under "Google". But when I print the names of the containers sometimes it comes empty or null or something vague, like "Address Book". Is there any way to find out which container belongs to which account(local, facebook, google etc). Thanks in advance.

    CNContactStore *contactStore = [[CNContactStore alloc]init];
    NSArray *keysToFetch = @[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactIdentifierKey,CNContactMiddleNameKey,CNContactPhoneNumbersKey];
    NSError *error;
    NSArray *containers = [contactStore containersMatchingPredicate:nil error:&error];
    for (CNContainer *container in containers) {
        NSLog(@"Container: %@",container.name);
    }
Kiran Sarvaiya
  • 1,318
  • 1
  • 13
  • 37
Rezwan
  • 477
  • 5
  • 19

3 Answers3

4

I spent 2 days on this issue but didn't get any solution.

Then do that with one patch. This is worked for me.

Objective C

if ([container.name isEqualToString:@"Card"]) {
    NSLog(@"iCloud");
} else if ([container.name isEqualToString:@"Address Book"]) {
    NSLog(@"google");
} else if ([container.name isEqualToString:@"Contacts"]) {
    NSLog(@"Yahoo");
} else {
    NSLog(@"Other");
}

Swift 5.3

if container.name == "Card" {
    print("iCloud")
} else if container.name == "Address Book" {
    print("google")
} else if container.name == "Contacts" {
    print("Yahoo")
} else {
    print("Other")
}
Ronak Kalavadia
  • 379
  • 2
  • 10
  • I wouldn't return "Yahoo" in that way, as "Contacts" is also used as the name of Outlook.com containers (which are also CardDAV) – Peter Johnson Apr 04 '22 at 14:58
2

The debugger shows that CNContainer has a member named accountIdentifier, but it always seems to be null. I'm wondering if it's associated with some Apple-private API that Contacts.app uses to obtain account names to use as container labels. Which is to say, for some reason it doesn't seem that Apple wants us to have this information. Maybe it's more useful for Exchange containers, where the container name functions as a group name.

JLundell
  • 1,580
  • 11
  • 14
-2

Initialising the array worked for me:

NSArray *containers = [[NSArray alloc] init];

Kevin
  • 1