要访问系统的通讯录,首先需要添加 AddressBook.framework
和 AddressBookUI.framework
两个框架到你工程中build
phase的"Link Binary With Libraries"之下,然后就可以开始了。
首先我们需要创建一个控制器:ViewController,在.h文件中导入头文件: <AddressBook/AddressBook.h>、 <AddressBookUI/AddressBookUI.h>,
#import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h>
然后在控制器实现 ABPeoplePickerNavigationControllerDelegate协议
@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate>
-(void)chooseContacts{
_personPickVC = [[ABPeoplePickerNavigationController alloc] init];
_personPickVC.peoplePickerDelegate = self;
_personPickVC.displayedProperties = @[[NSNumbernumberWithInt:kABPersonPhoneProperty]];
[self presentViewController:_personPickVC animated:YEScompletion:NULL];
}
//适配IOS8
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0) {
[peoplePicker dismissViewControllerAnimated:YES completion:^{
ABMultiValueRef phones =ABRecordCopyValue(person,kABPersonPhoneProperty);
long index = ABMultiValueGetIndexForIdentifier(phones,identifier);
NSString *phoneNO = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, index);
NSMutableString *mutableStr = [[NSMutableString alloc] init];
NSCharacterSet *set = [NSCharacterSetcharacterSetWithCharactersInString:@" -"];
NSArray *arr = [phoneNOcomponentsSeparatedByCharactersInSet:set];
for (NSString *str in arr) {
[mutableStr appendString:str];
}
_phoneNumtextField.text = mutableStr;
[peoplePicker dismissViewControllerAnimated:YES completion:^{ }];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex < actionSheet.numberOfButtons -1) {
_phoneNumtextField.text = _phoneNumbers[buttonIndex];
}
}
// 进入选择联系人界面
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
[peoplePicker dismissViewControllerAnimated:YES completion:^{
ABMultiValueRef phones =ABRecordCopyValue(person,kABPersonPhoneProperty);
//多个联系人号码处理
[_phoneNumbers removeAllObjects];
for (int i = 0; i < ABMultiValueGetCount(phones); i++) {
NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, i));
NSLog(@"号码:%@",phone);
//因为号码中可能含有空格或者- 所以需要分割
NSMutableString *mutableStr = [[NSMutableString alloc] init];
NSCharacterSet *set = [NSCharacterSetcharacterSetWithCharactersInString:@" -"];
NSArray *arr = [phonecomponentsSeparatedByCharactersInSet:set];
for (NSString *str in arr) {
[mutableStr appendString:str];
}
[_phoneNumbers addObject:mutableStr];
}
if ([_phoneNumbers count] >1) {
UIActionSheet *phoneAS = [[UIActionSheet alloc] initWithTitle:nildelegate:self cancelButtonTitle:nil destructiveButtonTitle:nilotherButtonTitles: nil];
for (NSString *phoneStr in _phoneNumbers) {
[phoneAS addButtonWithTitle:phoneStr];
}
[phoneAS addButtonWithTitle:@"取消"];
phoneAS.cancelButtonIndex = phoneAS.numberOfButtons -1;
phoneAS.actionSheetStyle =UIActionSheetStyleDefault; //chenyy可支持三种颜色样式
[phoneAS showInView:self.view];
}else{
_phoneNumtextField.text = _phoneNumbers[0];
}
}];
return YES;
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
[peoplePicker dismissViewControllerAnimated:YES completion:^{
}];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
return YES;
}