3

我需要给用户机会从通讯录中选择电话号码,所以我以苹果手册为例。但它只需要第一个数字,我怎么做才能让用户在通讯录中选择一个数字。

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

5 回答 5

4

我用它来显示电话号码列表,以便我的用户可以选择一个:

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

编辑:针对 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];
}
于 2013-06-28T07:48:58.570 回答
2

这将返回 Array,而不是包含该人拥有的所有 Number。之后,您可以从数组中选择任何数字。

- (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;
}
于 2013-06-28T07:46:24.787 回答
1

联系联系人;

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

要返回应用程序(关闭联系人视图):

- (void)peoplePickerNavigationControllerDidCancel: (ABPeoplePickerNavigationController *)peoplePicker {
    [self dismissModalViewControllerAnimated:YES];
}
于 2013-06-28T07:42:00.097 回答
0

使用peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier:委托方法,它为您提供有关所选字段的更多信息。

于 2013-06-28T07:23:44.813 回答
0

以下代码可能对您有所帮助:

-(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;
}
于 2013-08-12T14:07:30.003 回答