The contact information in the iPhone is stored in the system database. Each record in the database is an abrecordref instance.
The address book mainly stores two types of records:
1. Contact information: it is of the abperson type. It mainly includes the contact name, phone number, and address information.
2. group information: it is of the abgroup type. Used to group contacts to different groups. It has only one attribute, indicating the group name.
Add the following code to viewdidload to obtain information about all contacts and groups:
ABAddressBookRef addressBook =ABAddressBookCreate();CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople (addressBook);CFArrayRef allGroups = ABAddressBookCopyArrayOfAllGroups(addressBook);for (id person in (NSArray *) allPeople)[self logContact:person];for (id group in (NSArray *) allGroups)[self logGroup:group];CFRelease(allGroups);CFRelease(allPeople);CFRelease(addressBook);
Use abaddressbookcreate to create an addressbook instance. Abaddressbook copyarrayofallpeople and abaddressbook copyarrayofallgroups query all contacts and group information. Use the logperson and loggroup methods to output information to the console through a loop.
Logperson method:
CFStringRef name = ABRecordCopyCompositeName(person);
ABRecordID recId = ABRecordGetRecordID(person);
NSLog(@"Person Name: %@ RecordID:%d",name, recId);
Loggroup method:
CFStringRef name = ABRecordCopyValue(group,kABGroupNameProperty);
ABRecordID recId = ABRecordGetRecordID(group);
NSLog(@"Group Name: %@ RecordID:%d",name, recId);
Summary:This article briefly describes how to retrieve the address book information of the iPhone through the SDK.