一 . 帶UI
#import <ContactsUI/ContactsUI.h>
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//1. 創建聯系人控制器
CNContactPickerViewController *contactPickerVC = [[CNContactPickerViewController alloc] init];
//2. 設置代理 -->獲取數據
contactPickerVC.delegate = self;
//3.模態彈出視圖 -->
[self presentViewController:contactPickerVC animated:true completion:nil];
}
#pragma mark - 通訊錄代理方法/**點擊取消按鈕 會調用*/- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker { NSLog(@"cancel");}/**
選擇某個聯系人會調用 - contact頭文件中有詳細的電話信息*/
- (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);
}
}
/**選中多個聯系人的方法 -- 暫時沒什么用 這個方法如果實現 選擇單個聯系人的方法會被忽略!!!*/
- (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. 獲取授權狀態
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
//2.處理未決定狀態
if (status == CNAuthorizationStatusNotDetermined) {
[[CNContactStore new] requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
//2.1 判斷授權成功
if (granted) {
//2.2 獲取數據
[self onGetContactInfo];
}else {
NSLog(@"授權失敗");
}
}];
return;
}
//3 處理其他情況
if (status == CNAuthorizationStatusAuthorized) {
[self onGetContactInfo];
return;
}else {
NSLog(@"請在設置中打開");
}
}
#pragma mark - 獲取數據的方法
- (void)onGetContactInfo {
//二. 獲取信息
//1.聯系人信息抓取請求
//cnkey :聯系人信息是由多個屬性組成的 姓/名/電話 每個屬性都有一個字符串的key
CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactPhoneNumbersKey]];
//2.根據請求枚舉數據
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的兩個類庫- AddressBookUI.AddressBook
#import <AddressBookUI/AddressBookUI.h>
#pragma mark - 顯示聯系人界面
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//1.創建聯系人選擇導航控制器
ABPeoplePickerNavigationController *picker = [ABPeoplePickerNavigationController new];
//2.設置代理 - 一定不要寫delegate
picker.peoplePickerDelegate = self;
//3.模態彈出控制器
[self presentViewController:picker animated:true completion:nil];
}
#pragma mark - 代理方法
/**選中聯系人會調用的方法*/
- (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 {
}