iOS 通过AddressBookUI来获取通讯录
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
ABPeoplePickerNavigationController *pickController = [[ABPeoplePickerNavigationController alloc]init];
pickController.peoplePickerDelegate = self;
[self presentViewController:pickController animated:YES completion:^{
}];
}
//选中一个联系人的代理方法(实现这个方法后就不会进入联系人的详细界面)
-
(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person{
NSLog(@"====%@",person);
CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);// 这样的桥接方式将cf的转为foundtion后就不用再管理内存了。
NSString first = (__bridge_transfer NSString)(firstName);
NSLog(@"%@",first);
// 获取联系人的电话号码(获取到的是个数组,而且数组里面存放的好像是字典)
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex count = ABMultiValueGetCount(phone);
for (int i = 0 ; i<count; i++) {
// 获取手机号码的的名字
NSString phoneName= (__bridge_transfer NSString)(ABMultiValueCopyLabelAtIndex(phone, i));
// 获取手机号码
NSString phoneVaule = (__bridge_transfer NSString)(ABMultiValueCopyValueAtIndex(phone, i));
NSLog(@"==%@,%@",phoneName,phoneVaule);
}
CFRelease(phone);
}
//选中一个联系人的一个属性时的方法候调用的方法(如果实现了这个方法选择联系人的属性就会跳出这个页面,自动退出当前的控制器)。
-
(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
// 在这里面也是对person 这个对像进行操作和,上面的代理操作类似
}
// 点击取消按钮会执行的方法
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
NSLog(@"你点击了取消按钮");
}