In this how to series, I have come up with yet another code snippet, which allows you to use iPhone’s contact data into your own application. The code is:

ABAddressBookRef myAddressBook = ABAddressBookCreate();
NSArray *allPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(myAddressBook);
contactList = [[NSMutableArray alloc]initWithCapacity:[allPeople count]];
for (id record in allPeople) {
CFTypeRef phoneProperty = ABRecordCopyValue((ABRecordRef)record, kABPersonPhoneProperty);
NSArray *phones = (NSArray *)ABMultiValueCopyArrayOfAllValues(phoneProperty);
//NSLog(@”phones array: %@”, phones);
NSString* contactName = (NSString *)ABRecordCopyCompositeName((ABRecordRef)record);
NSMutableDictionary *newRecord = [[NSMutableDictionary alloc] init];
[newRecord setObject:contactName forKey:@”name”];
//[contactName release];
NSMutableString *newPhone = [[NSMutableString alloc] init];
for (NSString *phone in phones) {
//NSString *fieldData = [NSString stringWithFormat:@”%@: %@”, contactName, phone];
if(![newPhone isEqualToString:@””])
[newPhone appendString:@”, “];
[newPhone appendString:phone];
[newRecord setObject:newPhone forKey:@”phone”];
[newPhone release];
[phones release];
[contactList addObject:newRecord];
//[newPhone release];
// NSLog(@”Final data: %@”, contactList);

Leave a Reply

Your email address will not be published. Required fields are marked *