开发者

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

开发者 https://www.devze.com 2023-02-04 18:25 出处:网络
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.

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 gettin开发者_如何学编程g the names under which these different address books are saved and accessing their contact information.

Thank you!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号