两种方式
第一种:需要用户授权,通过代码就可以获取所有通讯录信息
第二种:iOS 9.0 以后,可以无需用户授权,直接打开通讯录选择界面,单选或者多选联系人,以获取信息。
一、用户授权,获取所有联系人信息
1. 在 info.plist 添加授权信息
2. 获取用户授权
申请使用通讯录,在无权限时提示用户打开权限
导入 #import
- (IBAction)getMailListAction:(id)sender {
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusNotDetermined) {
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError* _Nullable error) {
if (error) {
//无权限
[self showAlertViewAboutNotAuthorAccessContact];
} else {
//有权限
[self openContact];
}
}];
} else if(status == CNAuthorizationStatusRestricted) {
//无权限
[self showAlertViewAboutNotAuthorAccessContact];
} else if (status == CNAuthorizationStatusDenied) {
//无权限
[self showAlertViewAboutNotAuthorAccessContact];
} else if (status == CNAuthorizationStatusAuthorized) {
//有权限
[self openContact];
}
}
- (void)showAlertViewAboutNotAuthorAccessContact{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请授权通讯录权限" message:@"请在iPhone的\"设置-隐私-通讯录\"选项中,允许花解解访问你的通讯录" preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:OKAction];
[self presentViewController:alertController animated:YES completion:nil];
}
3. 获取所有联系人信息
- (void)openContact{
NSArray *keysToFetch = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch];
CNContactStore *contactStore = [[CNContactStore alloc] init];
[contactStore enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
NSString * firstName = contact.familyName;
NSString * lastName = contact.givenName;
//电话
NSArray * phoneNums = contact.phoneNumbers;
CNLabeledValue *labelValue = phoneNums.firstObject;
NSString *phoneValue = [labelValue.value stringValue];
NSLog(@"姓名:%@%@ 电话:%@", firstName, lastName, phoneValue);
}];
}
二、无需授权,获取用户选中的联系人信息
1. 打开通讯录界面
导入 #import
@interface ViewController ()
@end
@implementation ViewController {
CNContactPickerViewController * _peoplePickVC;
}
- (IBAction)openMailListAction:(id)sender {
_peoplePickVC = [[CNContactPickerViewController alloc] init];
_peoplePickVC.delegate = self;
[self showViewController:_peoplePickVC sender:nil];
}
注意 CNContactPickerViewController 对象需要定义为全局变量。
2. 获取单个联系人信息
// 获取指定联系人 里面只log了第一个电话号码
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
//姓名
NSString * firstName = contact.familyName;
NSString * lastName = contact.givenName;
//电话
NSArray * phoneNums = contact.phoneNumbers;
CNLabeledValue *labelValue = phoneNums.firstObject;
NSString *phoneValue = [labelValue.value stringValue];
NSLog(@"姓名:%@%@ 电话:%@", firstName, lastName, phoneValue);
}
3. 获取单个联系人指定电话
// 获取指定电话
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {
//姓名
NSString * firstName = contactProperty.contact.familyName;
NSString * lastName = contactProperty.contact.givenName;
//电话
NSString * phoneNum = [contactProperty.value stringValue];
NSLog(@"姓名:%@%@ 电话:%@", firstName, lastName, phoneNum);
}
4. 获取多个联系人信息
//获取多个联系人 里面只log了每个联系人第一个电话号码
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray *)contacts {
//遍历
for (CNContact * contact in contacts) {
//姓名
NSString * firstName = contact.familyName;
NSString * lastName = contact.givenName;
//电话
NSArray * phoneNums = contact.phoneNumbers;
CNLabeledValue *labelValue = phoneNums.firstObject;
NSString *phoneValue = [labelValue.value stringValue];
NSLog(@"姓名:%@%@ 电话:%@", firstName, lastName, phoneValue);
}
}
- 只可实现一个代理方法,同时实现多个代理方法只会生效一个,导致弹出的联系人选择页面不是你想要的效果。
Demo地址:
https://github.com/muyan091115/MailList