Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
    }
like image 636
Rezwan Avatar asked Feb 10 '16 09:02

Rezwan


2 Answers

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")
}
like image 163
Ronak Kalavadia Avatar answered Oct 14 '22 22:10

Ronak Kalavadia


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.

like image 37
JLundell Avatar answered Oct 14 '22 21:10

JLundell