一 . 帶UI
#import <ContactsUI/ContactsUI.h>
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//1. 創(chuàng)建聯(lián)系人控制器
CNContactPickerViewController *contactPickerVC = [[CNContactPickerViewController alloc] init];
//2. 設(shè)置代理 -->獲取數(shù)據(jù)
contactPickerVC.delegate = self;
//3.模態(tài)彈出視圖 -->
[self presentViewController:contactPickerVC animated:true completion:nil];
}
#pragma mark - 通訊錄代理方法/**點(diǎn)擊取消按鈕 會(huì)調(diào)用*/- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker { NSLog(@"cancel");}/**
選擇某個(gè)聯(lián)系人會(huì)調(diào)用 - contact頭文件中有詳細(xì)的電話信息*/
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
//1. 獲取姓名
NSLog(@"xing: %@, ming: %@",contact.familyName,contact.givenName);
//2. 獲取電話
//NSArray<CNLabeledValue<CNPhoneNumber *> *>
for (CNLabeledValue *labeledValue in contact.phoneNumbers) {
CNPhoneNumber *phoneNumber = labeledValue.value;
NSLog(@"phone: %@",phoneNumber.stringValue);
}
}
/**選中多個(gè)聯(lián)系人的方法 -- 暫時(shí)沒什么用 這個(gè)方法如果實(shí)現(xiàn) 選擇單個(gè)聯(lián)系人的方法會(huì)被忽略!!!*/
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact *> *)contacts {
}
二.不帶UI
#import <Contacts/Contacts.h>
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
/** CNAuthorizationStatusNotDetermined = 0,
CNAuthorizationStatusRestricted,
CNAuthorizationStatusDenied,
CNAuthorizationStatusAuthorized */
//1. 獲取授權(quán)狀態(tài)
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
//2.處理未決定狀態(tài)
if (status == CNAuthorizationStatusNotDetermined) {
[[CNContactStore new] requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
//2.1 判斷授權(quán)成功
if (granted) {
//2.2 獲取數(shù)據(jù)
[self onGetContactInfo];
}else {
NSLog(@"授權(quán)失敗");
}
}];
return;
}
//3 處理其他情況
if (status == CNAuthorizationStatusAuthorized) {
[self onGetContactInfo];
return;
}else {
NSLog(@"請(qǐng)?jiān)谠O(shè)置中打開");
}
}
#pragma mark - 獲取數(shù)據(jù)的方法
- (void)onGetContactInfo {
//二. 獲取信息
//1.聯(lián)系人信息抓取請(qǐng)求
//cnkey :聯(lián)系人信息是由多個(gè)屬性組成的 姓/名/電話 每個(gè)屬性都有一個(gè)字符串的key
CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactPhoneNumbersKey]];
//2.根據(jù)請(qǐng)求枚舉數(shù)據(jù)
CNContactStore *contactStore = [CNContactStore new];
[contactStore enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
NSLog(@"%@ : %@",contact.givenName,contact.familyName);
for (CNLabeledValue *labeledValue in contact.phoneNumbers) {
CNPhoneNumber *phoneNumber = labeledValue.value;
NSLog(@"tel: %@",phoneNumber.stringValue);
}
}];
}
附:
適配iOS8的兩個(gè)類庫- AddressBookUI.AddressBook
#import <AddressBookUI/AddressBookUI.h>
#pragma mark - 顯示聯(lián)系人界面
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//1.創(chuàng)建聯(lián)系人選擇導(dǎo)航控制器
ABPeoplePickerNavigationController *picker = [ABPeoplePickerNavigationController new];
//2.設(shè)置代理 - 一定不要寫delegate
picker.peoplePickerDelegate = self;
//3.模態(tài)彈出控制器
[self presentViewController:picker animated:true completion:nil];
}
#pragma mark - 代理方法
/**選中聯(lián)系人會(huì)調(diào)用的方法*/
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person {
CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
//ABAddressBook框架,使用的是coreFoundation的語法 沒有arc ,所以遇到的Copy/alloc/create 需要釋放
CFRelease(firstName);
}
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
}