2

I'm trying to NSLog name and number from my contact in Address Book. I succeed to log the name, but I don't succeed to solve the problem with number.

Here is my code :

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
if (addressBook != NULL) {
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        if (granted) {
            CFArrayRef allNames = ABAddressBookCopyArrayOfAllPeople(addressBook);

            if (allNames != NULL) {
                NSMutableArray *names = [NSMutableArray array];
                for (int i = 0; i < CFArrayGetCount(allNames); i++) {
                    ABRecordRef group = CFArrayGetValueAtIndex(allNames, i);
                    CFStringRef name = ABRecordCopyCompositeName(group);
                    [names addObject:(__bridge NSString *)name];
                    CFRelease(name);
                }


                NSLog(@"names = %@", names);
                CFRelease(allNames);
            }
        }
        CFRelease(addressBook);
    });
}

Maybe I have to create NSDictionnary ? I don't know how to solve it...

Vjardel
  • 1,065
  • 1
  • 13
  • 28
  • possible duplicate of [How to get a Phone Number from an Address Book Contact (iphone sdk)](http://stackoverflow.com/questions/286207/how-to-get-a-phone-number-from-an-address-book-contact-iphone-sdk) – Andrew Hershberger Jul 25 '15 at 14:32

2 Answers2

1

You'll need to use ABRecordCopyValue(group, kABPersonPhoneProperty) which returns ABMultiValueRef. See https://stackoverflow.com/a/286281/171089 for more.

Community
  • 1
  • 1
Andrew Hershberger
  • 4,152
  • 1
  • 25
  • 35
1

The phone numbers are a ABMultiValueRef:

ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
if (phones != NULL) {
    for (NSInteger index = 0; index < ABMultiValueGetCount(phones); index++) {
        NSString *phone = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, index));
        NSString *label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, index));  // either kABHomeLabel or kABPersonPhoneMainLabel or ...
        // do something with `phone` and `label`
    }
    CFRelease(phones);
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044