今天写代码遇到了要调用系统通讯录,看了一些博客发现写的都是获取通讯录的内容,而不是调用系统的界面.
分享一下自己写的代码
第一步:引入
#import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h>
第二步:添加点击事件 创建一个通讯录界面 并以present的方式跳转
#pragma mark -- IBAction - (IBAction)buttonClicked:(id)sender { ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init]; peoplePicker.peoplePickerDelegate = self; [self presentViewController:peoplePicker animated:YES completion:nil]; }
第三步:实现代理方法ABPeoplePickerNavigationControllerDelegate
#pragma mark -- ABPeoplePickerNavigationControllerDelegate - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { ABMultiValueRef valuesRef = ABRecordCopyValue(person, kABPersonPhoneProperty); CFIndex index = ABMultiValueGetIndexForIdentifier(valuesRef,identifier); CFStringRef value = ABMultiValueCopyValueAtIndex(valuesRef,index); [self dismissViewControllerAnimated:YES completion:^{ self.textField.text = (__bridge NSString*)value; }]; }
这里需要说明一下的是 这里提供的方法开始适用于iOS8
时间: 2024-12-24 11:02:25