你如何从电话的地址簿中选择一个特定号码?

时间:2011-08-04 12:38:07

标签: iphone objective-c addressbook

如何从电话的地址簿中选择某个号码?

我从地址簿中收到联系人,但只检索了手机号码。如何让用户选择手机/家庭/其他号码?

这是我的代码:

-(IBAction)pickContact
{
    // creating the picker
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    // place the delegate of the picker to the controll
    picker.peoplePickerDelegate = self;

    // showing the picker
    [self presentModalViewController:picker animated:YES];
    // releasing
    [picker release];

}
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
    // assigning control back to the main controller
    [self dismissModalViewControllerAnimated:YES];
}

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

    Contact *cont=[[Contact alloc] init];

    // setting the first name
    cont.fName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

    // setting the last name
    cont.lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);   

    // setting the number
    /*
     this function will set the first number it finds

     if you do not set a number for a contact it will probably
     crash
     */



    //ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);

    //  cont.number = (NSString*)ABMultiValueCopyValueAtIndex(multi, 0);
    ABMultiValueRef phones =(NSString*)ABRecordCopyValue(
                                                         person, kABPersonPhoneProperty);

    CFIndex numPhoneNums = ABMultiValueGetCount(phones);
    if(numPhoneNums == 0) {
        NSLog(@"No number available");
        cont.number = @"No number available";
    } else {
        cont.number = (NSString*) ABMultiValueCopyValueAtIndex(phones, 0);
    }

......其余的工作正常。

2 个答案:

答案 0 :(得分:2)

获取所有类型的电话号码...(人是ABRecordRef的一个实例)

    NSMutableArray *arPhList = [[NSMutableArray alloc] init];   
    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
    for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
    {       
        CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);   
        NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phones, j));
        NSString *phoneNumber = (NSString *)phoneNumberRef;            
        NSDictionary *dicTemp = [[NSDictionary alloc]initWithObjectsAndKeys:phoneNumber,@"value", phoneLabel,@"label", nil];  
        [arPhList addObject:dicTemp];
}   

答案 1 :(得分:0)

else {
        cont.number = (NSString*) ABMultiValueCopyValueAtIndex(phones, **0**);
    }

“电话”包含所有电话号码(移动电话,家庭电话,传真等)

但您只使用索引 0

您只使用第一个电话号码。 (这不会移动。)

使用for循环(numPhoneNums)访问所有数字。