iPhone: How do you get the names of all the address books on the iPhone?

时光总嘲笑我的痴心妄想 提交于 2019-12-04 15:47:48
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef sourcesArray = ABAddressBookCopyArrayOfAllSources(addressBook);
for (CFIndex i = 0; i < CFArrayGetCount(sourcesArray); i++) {
    ABRecordRef source = (ABRecordRef)CFArrayGetValueAtIndex(sourcesArray, i);
    ABRecordID sourceID = ABRecordGetRecordID(source);
    CFNumberRef sourceType = (CFNumberRef)ABRecordCopyValue(source, kABSourceTypeProperty);
    CFStringRef sourceName = (CFStringRef)ABRecordCopyValue(source, kABSourceNameProperty);
    NSLog(@"source id=%d type=%d name=%@", sourceID, [(NSNumber *)sourceType intValue], sourceName);
    CFRelease(sourceType);
    if (sourceName != NULL) CFRelease(sourceName); // some source names are NULL
}
CFRelease(sourcesArray);
CFRelease(addressBook);

Note that, as of iOS 4, not all sources return a name. You may provide your own names based on type.

Use ABAddressBookCopyArrayOfAllPeopleInSource(addressBook, source) to get entries in a source.

There is only a single, centralized address book database on iOS accessible by a set of C functions, see ABAddressBook and the iOS address book programming guide.

You may be referring to groups within that address book. In that case, you can get the list of groups using the ABAddressBookCopyArrayOfAllGroups function as described in the reference on ABGroup.

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