一.iOS9之前:AddressBookUI
的使用:
1.导入头文件,遵守协议
#import "ViewController.h"
#import
@interface ViewController ()
@end
2.创建选择联系人的控制器
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.创建选择联系人的控制器
ABPeoplePickerNavigationController *ppnc = [[ABPeoplePickerNavigationController alloc] init];
// 2.设置代理
ppnc.peoplePickerDelegate = self;
// 3.弹出控制器
[self presentViewController:ppnc animated:YES completion:nil];
}
3.实现代理方法
#pragma mark -
// 当用户选中某一个联系人时会执行该方法,并且选中联系人后会直接退出控制器
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
// 1.获取选中联系人的姓名
CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
// (__bridge NSString *) : 将对象交给Foundation框架的引用来使用,但是内存不交给它来管理
// (__bridge_transfer NSString *) : 将对象所有权直接交给Foundation框架的应用,并且内存也交给它来管理
NSString *lastname = (__bridge_transfer NSString *)(lastName);
NSString *firstname = (__bridge_transfer NSString *)(firstName);
NSLog(@"%@ %@", lastname, firstname);
// 2.获取选中联系人的电话号码
// 2.1.获取所有的电话号码
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex phoneCount = ABMultiValueGetCount(phones);
// 2.2.遍历拿到每一个电话号码
for (int i = 0; i < phoneCount; i++) {
// 2.2.1.获取电话对应的key
NSString *phoneLabel = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
// 2.2.2.获取电话号码
NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"%@ %@", phoneLabel, phoneValue);
}
// 注意:管理内存
CFRelease(phones);
}
// 当用户选中某一个联系人的某一个属性时会执行该方法,并且选中属性后会退出控制器
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
NSLog(@"%s", __func__);
}
二.iOS9之前:AddressBook
的使用
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.获取授权状态
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
// 2.如果是已经授权,才能获取联系人
if (status != kABAuthorizationStatusAuthorized) return;
// 3.创建通信录对象
ABAddressBookRef addressBook = ABAddressBookCreate();
// 4.获取所有的联系人
CFArrayRef peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex peopleCount = CFArrayGetCount(peopleArray);
// 5.遍历所有的联系人
for (int i = 0; i < peopleCount; i++) {
// 5.1.获取某一个联系人
ABRecordRef person = CFArrayGetValueAtIndex(peopleArray, i);
// 5.2.获取联系人的姓名
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSLog(@"%@ %@", lastName, firstName);
// 5.3.获取电话号码
// 5.3.1.获取所有的电话号码
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex phoneCount = ABMultiValueGetCount(phones);
// 5.3.2.遍历拿到每一个电话号码
for (int i = 0; i < phoneCount; i++) {
// 1.获取电话对应的key
NSString *phoneLabel = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
// 2.获取电话号码
NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"%@ %@", phoneLabel, phoneValue);
}
CFRelease(phones);
}
CFRelease(addressBook);
CFRelease(peopleArray);
}
三.iOS9之后:ContactsUI
的使用
1.创建选择联系人的控制器
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.创建选择联系人的控制器
CNContactPickerViewController *contactVc = [[CNContactPickerViewController alloc] init];
// 2.设置代理
contactVc.delegate = self;
// 3.弹出控制器
[self presentViewController:contactVc animated:YES completion:nil];
}
2.实现代理方法
#pragma mark -
// 当选中某一个联系人时会执行该方法
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact
{
// 1.获取联系人的姓名
NSString *lastname = contact.familyName;
NSString *firstname = contact.givenName;
NSLog(@"%@ %@", lastname, firstname);
// 2.获取联系人的电话号码
NSArray *phoneNums = contact.phoneNumbers;
for (CNLabeledValue *labeledValue in phoneNums) {
// 2.1.获取电话号码的KEY
NSString *phoneLabel = labeledValue.label;
// 2.2.获取电话号码
CNPhoneNumber *phoneNumer = labeledValue.value;
NSString *phoneValue = phoneNumer.stringValue;
NSLog(@"%@ %@", phoneLabel, phoneValue);
}
}
// 当选中某一个联系人的某一个属性时会执行该方法
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty
{
}
// 点击了取消按钮会执行该方法
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker
{
}
四.iOS9之后:Contacts
的使用
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.获取授权状态
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
// 2.判断授权状态,如果不是已经授权,则直接返回
if (status != CNAuthorizationStatusAuthorized) return;
// 3.创建通信录对象
CNContactStore *contactStore = [[CNContactStore alloc] init];
// 4.创建获取通信录的请求对象
// 4.1.拿到所有打算获取的属性对应的key
NSArray *keys = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
// 4.2.创建CNContactFetchRequest对象
CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
// 5.遍历所有的联系人
[contactStore enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
// 1.获取联系人的姓名
NSString *lastname = contact.familyName;
NSString *firstname = contact.givenName;
NSLog(@"%@ %@", lastname, firstname);
// 2.获取联系人的电话号码
NSArray *phoneNums = contact.phoneNumbers;
for (CNLabeledValue *labeledValue in phoneNums) {
// 2.1.获取电话号码的KEY
NSString *phoneLabel = labeledValue.label;
// 2.2.获取电话号码
CNPhoneNumber *phoneNumer = labeledValue.value;
NSString *phoneValue = phoneNumer.stringValue;
NSLog(@"%@ %@", phoneLabel, phoneValue);
}
}];
}