I succeed to add "firstName", "lastName", "phone" from Address Book contact in NSMutableDictionary.
Now I would like to display it on the table view : "firstName" + "lastName" but we can access the number of this contact. Maybe I have to create NSObject ?
Here is my code :
- (void)getContacts:(ABAddressBookRef)addressBook
{
NSArray *allData = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
NSInteger contactCount = [allData count];
for (int i = 0; i < contactCount; i++) {
ABRecordRef person = CFArrayGetValueAtIndex((__bridge CFArrayRef)allData, i);
NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
if (firstName) {
dictionary[@"firstName"] = firstName;
}
if (lastName) {
dictionary[@"lastName"] = lastName;
}
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex phoneNumberCount = ABMultiValueGetCount(phones);
if (phoneNumberCount > 0) {
NSString *phone = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, 0));
dictionary[@"phone"] = phone;
}
// or if you wanted to iterate through all of them, you could do something like:
//
// for (int j = 0; j < phoneNumberCount; j++) {
// NSString *phone = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, j));
//
// // do something with `phone`
// }
if (phones) {
CFRelease(phones);
}
self.test = [dictionary allValues];
NSLog(@"%@", self.test);
}
}
When I log self.test (NSArray) I have something like this :
2015-07-30 00:00:51.842 ChillN[4764:59116] (
John,
"888-555-5512",
Appleseed
)
2015-07-30 00:00:51.843 ChillN[4764:59116] (
Anna,
"555-522-8243",
Haro
)
I would like to display on my cell : "John Appleseed", "Anna Haro" but still store the number of this contact.
I don't know what to do next...