0

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...

Vjardel
  • 1,065
  • 1
  • 13
  • 28
  • You can combine the first and last name into a single string using `[NSString stringWithFormat:"%@ %@", dictionary[@"firstName"], dictionary[@"lastName"]]`. – deltacrux Jul 30 '15 at 00:50
  • @deltacrux thanks man, I knew it but how to display un CellForRow and store number with ? – Vjardel Jul 30 '15 at 07:40
  • I'm not sure what you mean by "store number with", but in your `cellForRowAtIndexPath` you can simply do `cell.textLabel.text = [NSString stringWithFormat:"%@ %@", dictionary[@"firstName"], dictionary[@"lastName"]]` to set the cell's text label to the person's full name. You'll first need to get the cell using `dequeueReusableCellWithIdentifier`. – deltacrux Jul 31 '15 at 00:06

0 Answers0