內(nèi)容概述
? ? iOS給我們提供了四個(gè)框架來供我們使用:iOS9之前,有AddressBookUI.framework和AddressBook.framework,iOS9推出的ContactsUI.framework和Contacts.framework根據(jù)名稱就可以區(qū)分一個(gè)有界面,一個(gè)無界面;
? ? Contacts框架相對AddressBook框架來說,肯定要簡單的多,如果不針對iOS9以前的版本進(jìn)行適配,建議盡量使用Contacts框架;
? ? 同時(shí)針對AddressBook框架,也有一個(gè)比較好用的三方框架供大家選擇使用
基本用法
1.AddressBookUI.framework
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event
{
// 1.獲取聯(lián)系人
// ABPeoplePickerNavigationController是iOS9之前的框架,所以在使用的時(shí)候,如果使用的是9.0+的版本,會報(bào)警告
ABPeoplePickerNavigationController *ppnc = [[ABPeoplePickerNavigationController alloc] init];
// 2.設(shè)置聯(lián)系人代理
ppnc.peoplePickerDelegate = self;
// 3.彈出聯(lián)系人界面
[self presentViewController:ppnc animated:YES completion:nil];
}
/**
*? 該方法在點(diǎn)擊聯(lián)系人的時(shí)候調(diào)用? 調(diào)用之后會自動退出控制器
*
*? @param peoplePicker 彈出來的聯(lián)系人控制器
*? @param person? ? ? 聯(lián)系人
*/
//- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
//{
//? ? // 1.獲取聯(lián)系人的姓名
//? ? CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
//? ? CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
//? ? // 注:這里存在內(nèi)存泄露
//? ? // (__bridge NSString *)這種橋接方式,只會將對象的所有權(quán)交給Foundation的引用使用
//? ? // (__bridge_transfer NSString *)這種橋接方式,對象所有權(quán)都交給Foundation的引用,并且內(nèi)存也交給它管理
//? ? // 因此如果用(__bridge NSString *)這種橋接方式,在最后釋放對象的時(shí)候,需要手動release掉
//? ? NSString *firstNamed = (__bridge_transfer NSString *)(firstName);
//? ? NSString *lastNamed = (__bridge_transfer NSString *)(lastName);
//
//? ? NSLog(@"%@ %@",lastNamed,firstNamed);
//
//? ? // 2.獲取聯(lián)系人的電話
//
//? ? // 2.1 獲取到所有電話
//? ? ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
//
//? ? CFIndex index = ABMultiValueGetCount(phones);
//? ? // 2.2 遍歷所有電話
//? ? for (int i = 0; i < index; i++) {
//? ? ? ? NSString *phoneNumber = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);
//? ? ? ? NSString *phoneLabel =(__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
//
//? ? ? ? NSLog(@"%@ %@",phoneLabel, phoneNumber);
//? ? }
//
//? ? // 釋放不需要的對象
//? ? CFRelease(phones);
//
//}
/**
*? 該方法是在進(jìn)入聯(lián)系人詳情界面的時(shí)候,點(diǎn)擊聯(lián)系人的各個(gè)屬性會調(diào)用該方法
*? 調(diào)用之后會自動退出控制器
*? 注:兩個(gè)代理方法不能同時(shí)調(diào)用? 這個(gè)方法調(diào)用的時(shí)候,不能調(diào)用上面的代理方法
*? @param peoplePicker 彈出來的聯(lián)系人控制器
*? @param person? ? ? 聯(lián)系人
*? @param property? ? 聯(lián)系人屬性
*? @param identifier? 標(biāo)識
*/
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
}
2.AddressBook.framework
在獲取到通訊錄之前,先要對用戶進(jìn)行授權(quán)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 判斷用戶是否授權(quán)
//? ? kABAuthorizationStatusNotDetermined? 未決定授權(quán)狀態(tài)
//? ? kABAuthorizationStatusRestricted? ? 受系統(tǒng)保護(hù),不能訪問通訊錄
//? ? kABAuthorizationStatusDenied,? ? ? ? 拒絕訪問
//? ? kABAuthorizationStatusAuthorized? ? 確認(rèn)授權(quán)
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
if (status == kABAuthorizationStatusNotDetermined) {
// 創(chuàng)建通信錄對象
ABAddressBookRef addressBook = ABAddressBookCreate();
// 請求授權(quán)
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
NSLog(@"授權(quán)成功");
}else
{
NSLog(@"授權(quán)失敗");
NSLog(@"%@",error);
}
});
}
return YES;
}
回到viewController,先判斷是否授權(quán),沒有授權(quán)就直接return掉
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event
{
// 獲取通訊錄的時(shí)候,先要判斷是否進(jìn)行授權(quán)
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
// 如果沒有進(jìn)行授權(quán)就return掉
if (status != kABAuthorizationStatusAuthorized) return;
// 獲取所有的聯(lián)系人
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
// 遍歷聯(lián)系人數(shù)組
CFIndex peopleCount = CFArrayGetCount(people);
for (int i = 0; i < peopleCount; i++) {
// 獲取聯(lián)系人姓名
ABRecordRef person = CFArrayGetValueAtIndex(people, i);
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSLog(@"%@ %@",lastName,firstName);
// 獲取聯(lián)系人電話
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex phoneCount = ABMultiValueGetCount(phones);
for (int i = 0; i < phoneCount; i++) {
// 獲取電話號碼
NSString *phoneNumber = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);
// 獲取電話標(biāo)簽
NSString *phoneLabel = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
NSLog(@"%@ %@",phoneLabel,phoneNumber);
}
CFRelease(phones);
}
CFRelease(addressBook);
CFRelease(people);
}
3.ContactsUI.framework
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event
{
//創(chuàng)建通訊錄界面
CNContactPickerViewController*cpvc = [[CNContactPickerViewControlleralloc]init];
//設(shè)置代理
cpvc.delegate=self;
//彈出通訊錄界面
[selfpresentViewController:cpvcanimated:YEScompletion:nil];
}
- (void)contactPicker:(CNContactPickerViewController*)picker didSelectContact:(CNContact*)contact
{
//直接根據(jù)contact對象獲取信息
NSString*firstName = contact.familyName;
NSString*lastName = contact.givenName;
NSLog(@"%@ ------ %@",lastName,firstName);
NSArray*phoneArray = contact.phoneNumbers;
for(CNLabeledValue*labelValueinphoneArray) {
CNPhoneNumber*value = labelValue.value;
NSString*phoneValue = value.stringValue;
NSString*phoneLabel = labelValue.label;
NSLog(@"%@ %@",phoneLabel,phoneValue);
}
}
4.Contacts.framework
在啟動頁,首先要進(jìn)行授權(quán)
//授權(quán)狀態(tài)
CNAuthorizationStatusstatus = [CNContactStoreauthorizationStatusForEntityType:CNEntityTypeContacts];
if(status ==CNAuthorizationStatusNotDetermined) {
//創(chuàng)建通訊錄對象
CNContactStore*store = [[CNContactStorealloc]init];
//給用戶授權(quán)
[storerequestAccessForEntityType:CNEntityTypeContactscompletionHandler:^(BOOLgranted,NSError*_Nullableerror) {
if(granted) {
NSLog(@"授權(quán)成功");
}else
{
NSLog(@"授權(quán)失敗:%@",error);
}
}];
}
授權(quán)之后,就開始獲取通訊錄信息
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event
{
//判斷是否授權(quán)
CNAuthorizationStatusstatus = [CNContactStoreauthorizationStatusForEntityType:CNEntityTypeContacts];
if(status !=CNAuthorizationStatusAuthorized)return;
//創(chuàng)建通信錄對象
CNContactStore*store = [[CNContactStorealloc]init];
//獲取所有的聯(lián)系人
NSArray*keys =@[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactPhoneNumbersKey];
CNContactFetchRequest*request = [[CNContactFetchRequestalloc]initWithKeysToFetch:keys];
[storeenumerateContactsWithFetchRequest:requesterror:nilusingBlock:^(CNContact*_Nonnullcontact,BOOL*_Nonnullstop) {
NSString*firstName = contact.familyName;
NSString*lastName = contact.givenName;
NSLog(@"%@ ------ %@",lastName,firstName);
NSArray*phoneArray = contact.phoneNumbers;
for(CNLabeledValue*labelValueinphoneArray) {
CNPhoneNumber*value = labelValue.value;
NSString*phoneValue = value.stringValue;
NSString*phoneLabel = labelValue.label;
NSLog(@"%@ %@",phoneLabel,phoneValue);
}
}];
}