How can I get container name using Contacts Framework (iOS9)
Asked Answered
R

3

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);
    }
Randi answered 10/2, 2016 at 9:18 Comment(2)
Interested if you've had any progress with this?Idolater
no I haven't so far.Randi
C
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")
}
Cogitation answered 13/7, 2017 at 5:14 Comment(1)
I wouldn't return "Yahoo" in that way, as "Contacts" is also used as the name of Outlook.com containers (which are also CardDAV)Plover
A
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.

Adapter answered 4/2, 2018 at 0:22 Comment(1)
@PeterJohnson’s comment is intended for Rona’s answer, I believe.Adapter
T
-3

Initialising the array worked for me:

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

Treaty answered 28/3, 2017 at 21:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.