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

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

...其余工作正常。

最佳答案

获取所有类型的电话号码...(人是 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];
}   

关于iphone - 如何从电话的地址簿中选择某个号码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6941531/

相关文章:

ios - 缩放 super View 时如何防止按钮调整大小?

objective-c - 从 NSThread 调用一个带有多个参数的方法

ios8 - iOS 8.0 和 9.0 中的地址簿和联系人框架

objective-c - 在 dispatch_queue sendComplete 上创建的 RACSubject 没有进入合并信号

iphone - iOS - 有清理 HTML 代码的好方法吗?

objective-c - Core Data 中的地址簿联系人

IOS Objective-c - 让联系人不起作用 - 没有请求提示 - 访问被拒绝

iphone - 检查设备iOS平台是否存在重载方法

iphone - NSData 到 NSString 的转换问题!

iphone - 每日 UILocaleNotification 的问题