Pavel Kaljunen
Pavel Kaljunen

Reputation: 1291

Get phonenumber from addressbook for two textfield

I followed Apple Address Book Programming Guide for iOS to get phonenumber from addressbook to my UITextField. But I have two UITextField and doesn't know how to implement same function to second textfield.

Presenting the people picker

- (IBAction)showPicker:(id)sender

{

    ABPeoplePickerNavigationController *picker =

            [[ABPeoplePickerNavigationController alloc] init];

    picker.peoplePickerDelegate = self;



    [self presentModalViewController:picker animated:YES];

}

Responding to user actions in the people picker

- (void)peoplePickerNavigationControllerDidCancel:

            (ABPeoplePickerNavigationController *)peoplePicker

{

    [self dismissModalViewControllerAnimated:YES];

}





- (BOOL)peoplePickerNavigationController:

            (ABPeoplePickerNavigationController *)peoplePicker

      shouldContinueAfterSelectingPerson:(ABRecordRef)person {



    [self displayPerson:person];

    [self dismissModalViewControllerAnimated:YES];



    return NO;

}



- (BOOL)peoplePickerNavigationController:

            (ABPeoplePickerNavigationController *)peoplePicker

      shouldContinueAfterSelectingPerson:(ABRecordRef)person

                                property:(ABPropertyID)property

                              identifier:(ABMultiValueIdentifier)identifier

{

    return NO;

}

Displaying a person’s information

- (void)displayPerson:(ABRecordRef)person

{

    NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person,

                                               kABPersonFirstNameProperty);

    self.firstName.text = name;



    NSString* phone = nil;

    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,

                                    kABPersonPhoneProperty);

    if (ABMultiValueGetCount(phoneNumbers) > 0) {

       phone = (__bridge_transfer NSString*)

               ABMultiValueCopyValueAtIndex(phoneNumbers, 0);

    } else {

        phone = @"[None]";

    }

    self.phoneNumber.text = phone;

}

EDIT

- (void)displayPerson:(ABRecordRef)person

{

    NSString* phone = nil;

    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

    if (ABMultiValueGetCount(phoneNumbers) > 0) {

        phone = (__bridge_transfer NSString*)

        ABMultiValueCopyValueAtIndex(phoneNumbers, 0);

    } else {

        phone = @"None";

    }


    self.phoneNumber.text = phone;



    if (ABMultiValueGetCount(phoneNumbers) > 1) {

        phone = (__bridge_transfer NSString*)

        ABMultiValueCopyValueAtIndex(phoneNumbers, 1);

    } else {

        phone = @"None";

    }

    self.phoneNumber2.text = phone;


}

enter image description here

Upvotes: 0

Views: 3074

Answers (1)

Rob
Rob

Reputation: 437987

I assume your second UITextField is for a second phone number, if it exists? You could then do:

if (ABMultiValueGetCount(phoneNumbers) > 1) {
   phone = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 1);
} else {
    phone = @"[None]";
}

self.phoneNumber2.text = phone;

Or you can use CFBridgingRelease instead of __bridge_transfer:

phone = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, 1));

And, by the way, wouldn't you also want to use ABMultiValueCopyLabelAtIndex to capture what type each phone number was (it doesn't help to know what a phone number is if you don't know what type of number it is). You could store these two phone number labels in two additional UITextFields (presumably next to their respective phone numbers).


Update:

I originally thought that you were looking for two phone numbers from the same contact. I'm now inferring from your user interface, where you have a "+" button next to each of the two phone number text fields that you want to tap on the button, and pull up the phone number for that text field. And then you'd then tap on the "+" button next to the other text field, and then pull up the phone number for a different contact and put it in that text field.

So, if this is the case, you might add an ivar to your class, such as:

int _phoneNumberIndex;

And then the first "+" button would have an IBAction like:

- (IBAction)showPicker1:(id)sender
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];

    picker.peoplePickerDelegate = self;

    _phoneNumberIndex = 1;

    [self presentModalViewController:picker animated:YES];
}

Whereas the second "+" button would have an IBAction like:

- (IBAction)showPicker2:(id)sender
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];

    picker.peoplePickerDelegate = self;

    _phoneNumberIndex = 2;

    [self presentModalViewController:picker animated:YES];
}

Then your displayPerson would be something like:

- (void)displayPerson:(ABRecordRef)person
{
    // first name

    NSString* name = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));

    self.firstName.text = name;

    // phone numbers

    NSString* phone;

    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

    // first phone number

    if (ABMultiValueGetCount(phoneNumbers) > 0)
        phone = CFBridingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, 0));
    else
        phone = @"[None]";

    if (_phoneNumberIndex == 1)
        self.phoneNumber.text = phone;
    else if (_phoneNumberIndex == 2)
        self.phoneNumber2.text = phone;
    else
        NSLog(@"Unrecognized _phoneNumberIndex");

    CFRelease(phoneNumbers);
}

By the way, while I think this does what you're looking for, it's not clear what you should be doing with the first name field (because you're picking two contacts, which first name do you want to use, or will you store the two first names in two fields, just like you do with phone numbers?). So, you'll have to look at that.

Clearly, if you didn't want two IBAction methods for the two "+" buttons, you could probably do a single one that looked at sender or it's tag, but that's not material to your question. Bottom line, use an ivar to designate which phone number you want to update.

Upvotes: 1

Related Questions