ios开发中直接选择一个联系人的电话号码和姓名

    xiaoxiao2021-03-25  12

    1、在AppDelegate中添加授权提示

    - (void)requestAuthorizationAddressBook { // 判断是否授权 ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus(); if (authorizationStatus == kABAuthorizationStatusNotDetermined) { // 请求授权 ABAddressBookRef addressBookRef = ABAddressBookCreate(); ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) { if (granted) { // 授权成功 } else { // 授权失败 NSLog(@"授权失败!"); } }); } }

    2、导入头文件和协议

    #import <AddressBookUI/ABPeoplePickerNavigationController.h>

    #import <AddressBook/ABPerson.h>

    #import <AddressBookUI/ABPersonViewController.h>

    <ABPeoplePickerNavigationControllerDelegate>

    3、访问并获取手机号和姓名

    - (void)getAddressBookClick{ // 1. 判读授权 ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus(); if (authorizationStatus != kABAuthorizationStatusAuthorized) { UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您设置允许APP访问您的通讯录\n设置-隐私-通讯录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alart show]; return; } ABPeoplePickerNavigationController *nav = [[ABPeoplePickerNavigationController alloc] init]; nav.peoplePickerDelegate = self; if([[UIDevice currentDevice].systemVersion floatValue]>=8.0){ nav.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false]; } [self presentViewController:nav animated:YES completion:nil]; } //取消选择 - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker { [peoplePicker dismissViewControllerAnimated:YES completion:nil]; } - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty); long index = ABMultiValueGetIndexForIdentifier(phone,identifier); NSString *phoneNO = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phone, index); if ([phoneNO hasPrefix:@"+"]) { phoneNO = [phoneNO substringFromIndex:3]; } phoneNO = [phoneNO stringByReplacingOccurrencesOfString:@"-" withString:@""]; [peoplePicker dismissViewControllerAnimated:YES completion:nil]; NSString *firstName=(__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)); NSString *lastName=(__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty)); NSString *allName; if ([CommandHelp checkStringIsNull:lastName] && [CommandHelp checkStringIsNull:firstName]) { allName = [NSString stringWithFormat:@"%@%@",lastName,firstName]; }else if([CommandHelp checkStringIsNull:firstName]){ allName = firstName; }else if ([CommandHelp checkStringIsNull:lastName]){ allName = lastName; } if (phone && [CommandHelp checkStringIsNull:phoneNO]) { NSLog(@"====name:%@ phoneNumber:%@",allName,phoneNO); // [self.tableView reloadData]; [peoplePicker dismissViewControllerAnimated:YES completion:nil]; return; } } - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person { ABPersonViewController *personViewController = [[ABPersonViewController alloc] init]; personViewController.displayedPerson = person; [peoplePicker pushViewController:personViewController animated:YES]; }

    效果:

    转载请注明原文地址: https://ju.6miu.com/read-156083.html

    最新回复(0)