如图所示,即为博主今天要说的功能,想必很多app都有出现,大家也见过类似的功能,网上的代码也是属于烂大街的东西,随便一搜就有一箩筐,这是博主的代码:
在使用下面代码之前,需要先引入: #import <AddressBookUI/ABPeoplePickerNavigationController.h> #import <AddressBook/ABPerson.h> #import <AddressBookUI/ABPersonViewController.h> 遵守ABPeoplePickerNavigationControllerDelegate协议 #pragma mark - addReceiverBtnAction - (void)addReceiverBtnAction { //点击➕触发事件,记得plist设置允许访问手机通讯录,不多说了。 ABPeoplePickerNavigationController *nav = [[ABPeoplePickerNavigationController alloc] init]; nav.peoplePickerDelegate = self; //这个方法是在iOS8.0时才出现的,在其后要增加if中的代码,否则点击联系人后会直接dismiss,错误信息如下: //[App] if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction if([[UIDevice currentDevice].systemVersion floatValue]>=8.0){ nav.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false]; } [self presentViewController:nav animated:YES completion:nil]; } //此处为选择通讯录联系人的代理回调 #pragma mark - ABPeoplePickerNavigationControllerDelegate //取消选择 - (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); //有些手机号码带@“+86”,所以从第三位取 if ([phoneNO hasPrefix:@"+"]) { phoneNO = [phoneNO substringFromIndex:3]; } //去除手机号中间的@“-” phoneNO = [phoneNO stringByReplacingOccurrencesOfString:@"-" withString:@""]; //这里的firstName和lastName指的是姓氏和名字,添加到通讯录时会有,但是这里有一个问题,博主在iOS10.3的系统上模拟器添加成功,但是在真机上这两个参数为nil,很匪夷所思的问题,好在有另一个方法,获取全名,如下anFullName: NSString *firstName=(__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)); NSString *lastName=(__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty)); [peoplePicker dismissViewControllerAnimated:YES completion:nil]; CFStringRef anFullName = ABRecordCopyCompositeName(person); UITextField *address_contentField = [self.view viewWithTag:100]; UITextField *address_contentField1 = [self.view viewWithTag:101]; NSLog(@"%@%@---%@",firstName,lastName,anFullName); if (phoneNO) { address_contentField1.text = phoneNO; addressMobile = phoneNO; } else address_contentField1.text = @""; if (anFullName) { address_contentField.text = [NSString stringWithFormat:@"%@",anFullName]; addressName = (__bridge NSString *)(anFullName); } else address_contentField.text = @""; } - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person { ABPersonViewController *personViewController = [[ABPersonViewController alloc] init]; personViewController.displayedPerson = person; [peoplePicker pushViewController:personViewController animated:YES]; } 到这里并没有结束,除了获取这些个参数外,也许满足了大部分人的需求,但是你不知道的是,我们还可以获取一些别的东西,我们知道通讯录并不只是姓名和电话,还有组织,工作,部门之类的信息,如果设置齐全,是可以一并获取的,方法参考下面: // Property keys AB_EXTERN const ABPropertyID kABPersonFirstNameProperty AB_DEPRECATED("use CNContact.givenName"); // First name - kABStringPropertyType AB_EXTERN const ABPropertyID kABPersonLastNameProperty AB_DEPRECATED("use CNContact.familyName"); // Last name - kABStringPropertyType AB_EXTERN const ABPropertyID kABPersonMiddleNameProperty AB_DEPRECATED("use CNContact.middleName"); // Middle name - kABStringPropertyType AB_EXTERN const ABPropertyID kABPersonPrefixProperty AB_DEPRECATED("use CNContact.namePrefix"); // Prefix ("Sir" "Duke" "General") - kABStringPropertyType AB_EXTERN const ABPropertyID kABPersonSuffixProperty AB_DEPRECATED("use CNContact.nameSuffix"); // Suffix ("Jr." "Sr." "III") - kABStringPropertyType AB_EXTERN const ABPropertyID kABPersonNicknameProperty AB_DEPRECATED("use CNContact.nickname"); // Nickname - kABStringPropertyType AB_EXTERN const ABPropertyID kABPersonFirstNamePhoneticProperty AB_DEPRECATED("use CNContact.phoneticGivenName"); // First name Phonetic - kABStringPropertyType AB_EXTERN const ABPropertyID kABPersonLastNamePhoneticProperty AB_DEPRECATED("use CNContact.phoneticFamilyName"); // Last name Phonetic - kABStringPropertyType AB_EXTERN const ABPropertyID kABPersonMiddleNamePhoneticProperty AB_DEPRECATED("use CNContact.phoneticMiddleName"); // Middle name Phonetic - kABStringPropertyType AB_EXTERN const ABPropertyID kABPersonOrganizationProperty AB_DEPRECATED("use CNContact.organizationName"); // Company name - kABStringPropertyType AB_EXTERN const ABPropertyID kABPersonDepartmentProperty AB_DEPRECATED("use CNContact.departmentName"); // Department name - kABStringPropertyType AB_EXTERN const ABPropertyID kABPersonJobTitleProperty AB_DEPRECATED("use CNContact.jobTitle"); // Job Title - kABStringPropertyType AB_EXTERN const ABPropertyID kABPersonEmailProperty AB_DEPRECATED("use CNContact.emailAddresses"); // Email(s) - kABMultiStringPropertyType AB_EXTERN const ABPropertyID kABPersonBirthdayProperty AB_DEPRECATED("use CNContact.birthday"); // Birthday associated with this person - kABDateTimePropertyType AB_EXTERN const ABPropertyID kABPersonNoteProperty AB_DEPRECATED("use CNContact.note"); // Note - kABStringPropertyType AB_EXTERN const ABPropertyID kABPersonCreationDateProperty AB_DEPRECATED(""); // Creation Date (when first saved) AB_EXTERN const ABPropertyID kABPersonModificationDateProperty AB_DEPRECATED(""); // Last saved date // Addresses AB_EXTERN const ABPropertyID kABPersonAddressProperty AB_DEPRECATED("use CNContact.postalAddresses"); // Street address - kABMultiDictionaryPropertyType //这些都是系统自带的一些属性,学习的小伙伴可以一个个替换来查看。
这篇博文就到此结束了,要说没什么难度,都是苹果API给出的方法,套路罢了,用过就知道了。