調(diào)起
eg:
//ContactsUI.framework中
CNContactPickerViewController *contactVC = [CNContactPickerViewController new];
contactVC.delegate = self;
[self presentViewController:contactVC animated:YES completion:^{
}];
添加授權(quán)
CNContactStore *contactStore = [[CNContactStore alloc] init];
[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
// 成功
} else {
// 失敗
}
}];
代理方法
#pragma mark - CNContactViewControllerDelegate代理
//選擇一個(gè)聯(lián)系人的時(shí)候調(diào)用
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
//1.姓名
NSLog(@"%@-%@",contact.givenName,contact.familyName);
//2.獲取電話 --->泛型,會(huì)在遍歷數(shù)組幫很大忙。
for (CNLabeledValue *labelValue in contact.phoneNumbers) {
NSLog(@"電話標(biāo)簽: %@",labelValue.label);
CNPhoneNumber *phoneNumber = labelValue.value;
NSLog(@"電話號(hào)碼: %@",phoneNumber.stringValue);
}
}
//實(shí)現(xiàn)此方法就可以選擇多個(gè)聯(lián)系人梧宫,如果不實(shí)現(xiàn)則是單選聯(lián)系人
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact *> *)contacts{
for (CNContact *contact in contacts) {
//1.姓名
NSLog(@"%@-%@",contact.givenName,contact.familyName);
//2.獲取電話 --->泛型,會(huì)在遍歷數(shù)組幫很大忙笼蛛。
for (CNLabeledValue *labelValue in contact.phoneNumbers) {
NSLog(@"電話標(biāo)簽: %@",labelValue.label);
CNPhoneNumber *phoneNumber = labelValue.value;
NSLog(@"電話號(hào)碼: %@",phoneNumber.stringValue);
}
NSLog(@"\n\n");
}
}
//選擇聯(lián)系人屬性
//- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty{
//
//}
//- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperties:(NSArray<CNContactProperty *> *)contactProperties{
//
//}
//取消選擇聯(lián)系人的時(shí)候調(diào)用
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker{
}