iphoneiosobjective-cabpeoplepickerview

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);
}

Solution

  • 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];
    }