2

I started developing a settings page for my app.

On this page, the user can tap a "+" button which will open the ABPeoplePickerNavigationController. When the contact is tapped, the text fields on the settings page will be filled appropriately with the correct data from the chosen user.

I understand that if I want to retrieve someone's work email, it's:

NSString *workEmail = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, 1);

and for home it would be:

NSString *homeEmail = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, 0);

But as far as retrieving different types of phone numbers, I am stuck.

I would appreciate it much if someone could tell me how to get the different types of phone numbers hopefully similar to the way I get the two emails.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
doc92606
  • 691
  • 2
  • 11
  • 32

1 Answers1

7

Well, first off, you understand wrong--there's no guarantee that the user's home email address is #0. What if you only have that user's work email? Then that will be in slot 0.

What you want is ABMultiValueCopyLabelAtIndex(), which when used with named constants will tell you which is which:

ABPersonRef person = ...;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
if (phoneNumbers) {
    CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
    for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
        CFStringRef label = ABMultiValueCopyLabelAtIndex(phoneNumbers, i);
        if (label) {
            if (CFEqual(label, kABWorkLabel)) {
                /* it's the user's work phone */
            } else if (CFEqual(label, kABHomeLabel)) {
                /* it's the user's home phone */
            } else if (...) {
                /* other specific cases of your choosing... */
            } else {
                /* it's some other label, such as a custom label */
            }
            CFRelease(label);
        }
    }
    CFRelease(phoneNumbers);
}
Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
  • So inside the "if() brackets" I would put "self.workTextField.text = (__bridge NSString *)(kABWorkLabel); Is that correct? – doc92606 Jan 06 '13 at 01:01
  • If you want to display an unlocalized string representing the "work" label, then yes. This is probably not what you want to do though. – Jonathan Grynspan Jan 06 '13 at 01:26
  • Say i have a textfield that only wants to pick up the work email ONLY. – doc92606 Jan 06 '13 at 23:54
  • So you want to display the *value* of the email address, right? – Jonathan Grynspan Jan 07 '13 at 00:07
  • Yes, yes. email or phone. I'd like to pick up the value and display it in the textfield. – doc92606 Jan 07 '13 at 04:23
  • Then the body of the `if` statement would be something like `NSString *thePhoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i)); self.workTextField.text = thePhoneNumber;` Though this is getting away from your original question, which was how to determine which phone number corresponded to which label. – Jonathan Grynspan Jan 07 '13 at 23:49
  • Thanks very much. Your previous response was EXACTLY what I was looking for. Next time, I'll be more clear as to what I'm looking for in my questions.. – doc92606 Jan 09 '13 at 01:36