ABAddressBook ABSourceName
Asked Answered
A

1

6

How can I get the name of an ABAddressBook source to display it? (I know the enums kABSourceTypeLocal, kABSourceTypeExchange, ... )

I don't mean the source-type-name you get with ABRecordCopyValue(source,ABSourceNameProperty), but the real name which is shown in Apple's Contacts-App as a table section, for example: "Exchange Google" or "iCloud"

Azygous answered 8/5, 2012 at 8:6 Comment(2)
Same problem on my side. Have you been able to retrieve the name?Ibsen
Any success? Same problem here too.Rinee
P
2

If you check out the ABSource Reference, you can see that they have a property called kABSourceNameProperty that contains "the name of the source". Here's how you would get all the source names:

NSMutableArray *sourceNames = [[NSMutableArray alloc] init];
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef sourcesArray = ABAddressBookCopyArrayOfAllSources(addressBook);

for (CFIndex i = 0; i < CFArrayGetCount(sourcesArray); i++) {
    ABRecordRef source = (ABRecordRef)CFArrayGetValueAtIndex(sourcesArray, i);
    CFStringRef sourceName = (CFStringRef)ABRecordCopyValue(ABRecordGetRecordID(source, kABSourceNameProperty);

    if(sourceName){
        [sourceNames addObject: (__bridge_transfer NSString *)sourceName];
    }
}

CFRelease(sourcesArray);
CFRelease(addressBook);

Hope this helps!

Proselyte answered 2/7, 2012 at 19:59 Comment(2)
This does not give the same source names as Apple's contacts UI. For me, Apple's shows "Exchange", "Facebook" and "iCloud". Where as the above answer gives "Contacts" (CNContainerTypeExchange), "Card" (CNContainerTypeCardDAV) and "" (CNContainerTypeCardDAV)Inamorata
@MattToohey - This answer was posted over three years ago. I'm not up to date with Apple's latest changes to their Address Book API so this will most likely be broken/have unexpected results if you run it now.Proselyte

© 2022 - 2024 — McMap. All rights reserved.