通讯录取电话号码和联系人名字

如果 有人要做从通讯录 取联系人和电话号码的话,那么这篇文章可能会对你有帮助哟

首先要导入AddressBook.framework和AddressBookUI.framework 这两个框架。

然后

(1)创建并初始化一个ABPeoplePickerNavigationController对象

(2)设置其代理(delegate)

(3)用presentModalViewController:animated:这个方法进行显示整个通讯录页面


按钮点击事件

- (IBAction)connectAction:(UIButton *)sender

{

    ABPeoplePickerNavigationController *pickerNavController = [[ABPeoplePickerNavigationController alloc] init];

    pickerNavController.peoplePickerDelegate = self;

    [self presentViewController:pickerNavController animated:YES completion:nil];

}


实现peoplePickerDelegate的代理方法

#pragma mark - ABPeoplePickerNavigationControllerDelegate


//8.0

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person NS_AVAILABLE_IOS(8_0)

{

    ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);

    //    获取联系人姓名

    NSString *firstName=(__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);

    if (firstName==nil) {

        firstName = @" ";

    }

    NSString *lastName=(__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);

    if (lastName==nil) {

        lastName = @" ";

    }

    //获取联系人电话

    NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0];

    for (NSInteger i = 0; i < ABMultiValueGetCount(phoneMulti); i++) {

        

        NSString *aPhone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneMulti, i);

        

        [phones addObject:aPhone];

        

    }

    NSDictionary *dic=@{@"firstName": firstName,@"lastName":lastName,@"phones":phones[0]};

    NSLog(@"%@",dic);

    

    [peoplePicker dismissViewControllerAnimated:YES completion:^{

        _nickNameTextField.text = [NSString stringWithFormat:@"%@ %@", dic[@"firstName"], dic[@"lastName"]];

        _phoneNumTextField.text = [NSString stringWithFormat:@"%@",dic[@"phones"]];

    }];

}


- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker

{

    [peoplePicker dismissViewControllerAnimated:YES completion:nil];

}


//8.0之前

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

{

    ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);

    

    //    获取联系人姓名

    NSString *firstName=(__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);

    if (firstName==nil) {

        firstName = @" ";

    }

    NSString *lastName=(__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);

    if (lastName==nil) {

        lastName = @" ";

    }

    //获取联系人电话

    NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0];

    

    for (NSInteger i = 0; i < ABMultiValueGetCount(phoneMulti); i++) {

        

        NSString *aPhone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneMulti, i);

        

        [phones addObject:aPhone];

        

    }

    NSDictionary*dic=@{@"firstName": firstName,@"lastName":lastName,@"phones":phones[0]};

    NSLog(@"%@",dic);

    [peoplePicker dismissViewControllerAnimated:YES completion:^{

        _nickNameTextField.text = [NSString stringWithFormat:@"%@ %@", dic[@"firstName"], dic[@"lastName"]];

        _phoneNumTextField.text = [NSString stringWithFormat:@"%@",dic[@"phones"]];

    }];

    return NO;

    

}


//8.0之前

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker

      shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier

{

    return NO;

}


代理一共有五个方法

(1)peoplePickerNavigationControllerDidCancel:当用户选择取消时调用这个方法,可以在这个方法里取消整个通讯录页面的显示。

(2)8.0已经废弃 peoplePickerNavigationController:shouldContinueAfterSelectingPerson: 当用户选择了通讯录中某一个联系人时调用这个方法,可以在这里获取联系人的信息。如果希望可以继续显示这个联系人更具体的信息,则return YES。否则取消整个通讯录页面的显示并return NO。

(3)8.0已经废弃 peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier: 如果上一个方法返回的是YES,则会显示某一个联系人信息,如果选择了联系人的某一项纪录,就会调用这个方法,可以通过点击选择联系人的某一项信息。如果 希望可以对选择的某一项纪录进行进一步操作,比如直接拨打电话或调用邮箱发送邮件,则return YES。否则取消整个通讯录页面的显示并return NO。

(4)8.0使用peoplePickerNavigationController:didSelectPerson: 和(2)的操作差不多,不过没有返回,如果要跳出就直接dismiss

(5)8.0使用 peoplePickerNavigationController:didSelectPerson:property:identifier:和(3)的操作差不多。

因为我这里 不需要再进入详情页 所以 没有实现 (5)方法,希望对你有帮助。


你可能感兴趣的:(ios,通讯录,AddressBook)