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

丶灬走出姿态 提交于 2019-12-06 06:35:48

问题


Some users have multiple address books in their iPhone Contacts, as a result of different synchronization connections they have made with e.g. Exchange Servers.

How is it possible to get all of these different address books? I would be interested in getting the names under which these different address books are saved and accessing their contact information.

Thank you!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4680816/iphone-how-do-you-get-the-names-of-all-the-address-books-on-the-iphone

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