vZ10
vZ10

Reputation: 2676

Access to people info in iPhone address book

I need to give user opportunity to choose phone number from address book, so I took example from apple manual. But it takes only the first number, how I can make so user can choose one of one's numbers in address book.

- (IBAction)adressBook:(UIButton *)sender {
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;

    [self presentModalViewController:picker animated:YES];
}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
    [self dismissModalViewControllerAnimated:YES];
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person {

    [self displayPerson:person];
    [self dismissModalViewControllerAnimated:YES];

    return NO;
}

- (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.telNumber.text = phone;
    CFRelease(phoneNumbers);
}

Upvotes: 3

Views: 1941

Answers (5)

Rick
Rick

Reputation: 1818

I used this to show a list of phone numbers so my user can select one:

- (IBAction)getContact:(id)sender
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    picker.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]];
    [self presentViewController:picker animated:YES completion:nil];
}

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{    
    // ensure user picked a phone property
    if(property == kABPersonPhoneProperty)
    {
        ABMultiValueRef phone = ABRecordCopyValue(person, property);
        self.contactTextField.text = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phone, ABMultiValueGetIndexForIdentifier(phone, identifier));

        [self dismissModalViewControllerAnimated:YES];
    }
    else
        /* Display message if selection is not a phone number */

    return NO;
}

Edit: Updated for iOS 7 & iOS 8

// Delegate Method for iOS 7
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{    
    // ensure user picked a phone property
    if(property == kABPersonPhoneProperty)
    {
        ABMultiValueRef phone = ABRecordCopyValue(person, property);
        self.contactTextField.text = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phone, ABMultiValueGetIndexForIdentifier(phone, identifier));

        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else
        /* Display message if selection is not a phone number */

    return NO;
}

// Delegate Method for iOS 8
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    // Call the delegate method for iOS 7
    [self peoplePickerNavigationController:peoplePicker shouldContinueAfterSelectingPerson:person property:property identifier:identifier];
}

Upvotes: 4

Kalaichelvan
Kalaichelvan

Reputation: 220

Following code might help you:

-(IBAction)fromAddressBook {


ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;

[self presentViewController:picker animated: YES completion:NO];

 }



- (void)peoplePickerNavigationControllerDidCancel: (ABPeoplePickerNavigationController *)peoplePicker {
[self dismissViewControllerAnimated: YES completion:NO];
}





- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker
  shouldContinueAfterSelectingPerson:(ABRecordRef)person {



return YES;
}




- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
  shouldContinueAfterSelectingPerson:(ABRecordRef)person
                            property:(ABPropertyID)property
                          identifier:(ABMultiValueIdentifier)identifier
{

if (property == kABPersonPhoneProperty) { // if tapped is equal to a phone property
    CFStringRef cfnumber;
    ABMultiValueRef numbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
    for(CFIndex i = 0; i < ABMultiValueGetCount(numbers); i++) {
        if(identifier == ABMultiValueGetIdentifierAtIndex (numbers, i)) { //if tapped number identifier is the same as identifier number tapped
            cfnumber = ABMultiValueCopyValueAtIndex(numbers, i); // copy the number to CFSTRING number
        }
    }
    NSString *number = [NSString stringWithFormat:@"%@",cfnumber];
    CFRelease(cfnumber);

//do anything you want with the number. example,

    self.notesField.text = number ;

}


[self dismissViewControllerAnimated:YES completion:nil];



return NO;


}




-(BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifierForValue
{
if (property == kABPersonPhoneProperty)
{
    ABMultiValueRef numbers = ABRecordCopyValue(person, property);
    NSString* targetNumber = (__bridge NSString *) ABMultiValueCopyValueAtIndex(numbers, ABMultiValueGetIndexForIdentifier(numbers, identifierForValue));

    NSLog(@"%@", targetNumber);



}
return YES;
}

Upvotes: 0

Dilip Manek
Dilip Manek

Reputation: 9143

This will return Array than contain all the Number that person have. After that you can select any number from array.

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    //get the phone number
    ABMultiValueRef phone = (__bridge ABMultiValueRef)((__bridge NSMutableDictionary *)ABRecordCopyValue(person, kABPersonPhoneProperty));
    NSArray *phoneArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(phone);
    NSMutableString *strPhone = [NSMutableString string];

    for (int i=0; i<[phoneArray count]; i++)
    {
        [strPhone appendString:[NSString stringWithFormat:@"%@,",[phoneArray objectAtIndex:i]]];
    }

    NSLog(@"Dilip phoneArray : %@",phoneArray);
    NSLog(@"Dilip strPhone : %@",strPhone);

    phone = nil;
    phoneArray = nil;
    strPhone = nil;

    [peoplePicker dismissModalViewControllerAnimated:YES];
    return NO;
}

Upvotes: 2

Engnyl
Engnyl

Reputation: 1380

To get to the contacts;

- (IBAction)getContact:(id)sender{
    ABPeoplePickerNavigationController *pickerPhone =
    [[ABPeoplePickerNavigationController alloc] init];
    pickerPhone.peoplePickerDelegate = self;
    [self presentModalViewController:pickerPhone animated:YES];
    [pickerPhone release];
}

To get back to the application (dismiss contact view):

- (void)peoplePickerNavigationControllerDidCancel: (ABPeoplePickerNavigationController *)peoplePicker {
    [self dismissModalViewControllerAnimated:YES];
}

Upvotes: 1

Wain
Wain

Reputation: 119021

Use the peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier: delegate method which gives you more information about the field that was selected.

Upvotes: 0

Related Questions