iOS10 獲取系統通訊錄新方法
阿新 • • 發佈:2019-02-01
- 所需框架
#import <ContactsUI/ContactsUI.h>
- 1
- 1
- 遵循代理
CNContactPickerDelegate
- 1
- 1
- 呼叫通訊錄
- 如果在iOS10的機器上呼叫以前的ABPeoplePickerNavigationController老方法將直接崩潰.
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//iOS 10
// AB_DEPRECATED("Use CNContactPickerViewController from ContactsUI.framework instead")
CNContactPickerViewController * contactVc = [CNContactPickerViewController new];
contactVc.delegate = self;
[self presentViewController:contactVc animated:YES completion:^{
}];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 選擇完成代理回撥
#pragma mark - 使用者點選聯絡人獲取方法 兩個方法都寫只調用此方法
-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
// NSLog(@"contact : %@",contact);
// 姓氏 名字
NSLog(@"name:%@%@",contact.familyName,contact.givenName);
//公司名
NSLog(@"公司: %@",contact.organizationName);
//獲取通訊錄某個人所有電話並存入陣列中 需要哪個取哪個
NSMutableArray * arrMPhoneNums = [NSMutableArray array];
for (CNLabeledValue * labValue in contact.phoneNumbers ) {
NSString * strPhoneNums = [labValue.value stringValue];
NSLog(@"所有電話是: %@",strPhoneNums);
[arrMPhoneNums addObject:strPhoneNums];
}
//所有郵件地址陣列
NSMutableArray * arrMEmails = [NSMutableArray array];
for (CNLabeledValue * labValue in contact.emailAddresses) {
NSLog(@"email : %@",labValue.value);
[arrMEmails addObject:labValue.value];
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 使用者點進去獲取屬性呼叫方法 例如從通訊錄選擇聯絡人打電話兩個方法都寫只調用上面方法
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty{
// NSLog(@"contactProperty : %@",contactProperty);
// NSLog(@"contact : %@",contactProperty.contact);
// NSLog(@"key : %@",contactProperty.key);
// [[UIApplication sharedApplication] openURL:url];
// NSLog(@"identifier : %@",contactProperty.identifier);
// NSLog(@"label : %@",contactProperty.label);
//獲得點選的屬性,在此進行處理...
NSLog(@"value : %@",[contactProperty.value stringValue]);
[picker dismissViewControllerAnimated:YES completion:nil];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 取消選擇回撥
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}