iphone - 访问 iPhone 通讯录中的人员信息

标签 iphone ios objective-c abpeoplepickerview

我需要让用户有机会从地址簿中选择电话号码,所以我从苹果手册中举了个例子。但它只需要第一个号码,我如何才能让用户可以在地址簿中选择一个号码。

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

最佳答案

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

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

关于iphone - 访问 iPhone 通讯录中的人员信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17359296/

相关文章:

iphone - 在完成之前启动动画 UIView?

ios - cocos2d ios 如何播放背景音乐并知道当前播放时间?

iphone - 升级已提交到应用商店的应用的公司名称

iphone - 在 UIWebView 中呈现 html 时,iOS 应用程序变得无响应

iphone - 更改键盘中完成按钮的文本

ios - 当一个对象被分配给另一个对象时会发生什么?

ios - 在 iOS 上重新初始化 swift Realm DB 的正确方法?

iphone - 如何使用uinavigationcontroller

iphone - 如何从移动应用程序嗅探互联网连接?

iPhone:关闭数据