简体   繁体   中英

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

Some users have multiple address books in their iPhone Contacts, as a result of different synchronization connections they have made with eg 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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM