DE71B08E-B919-49FF-B6B0-5538A19ACBC3.png
在ios中,有兩個(gè)框架可以訪問(wèn)用戶(hù)的通訊錄
- AddressBookUI.framework
- 提供了聯(lián)系人列表界面, 聯(lián)系人詳情界面, 添加聯(lián)系人界面等
- 一般用于選擇聯(lián)系人
- AddressBook.framework
- 純C語(yǔ)言的API, 僅僅能獲取到聯(lián)系人數(shù)據(jù)
- 沒(méi)有提供UI界面展示, 需要自己搭建聯(lián)系人展示界面
- 里面的數(shù)據(jù)類(lèi)型大部分基于Core Foundation框架
- 從ios6開(kāi)始, 需要得到用戶(hù)的授權(quán)才能訪問(wèn)通訊錄, 因此在使用之前需要檢查用戶(hù)是否已經(jīng)授權(quán)(使用AddressBookUI默認(rèn)已經(jīng)授權(quán))
- 獲得通訊錄的授權(quán)狀態(tài): ABAddressBookGetAuthorizationStatus()
- kABAuthorizationStatusNotDetermined: 用戶(hù)還沒(méi)有決定是否授權(quán)你的應(yīng)用程序進(jìn)行訪問(wèn)
- kABAuthorizationStatusRestricted: iOS設(shè)備上一些許可配置阻止程序與通訊錄數(shù)據(jù)庫(kù)進(jìn)行交互
- kABAuthorizationStatusDenied: 用戶(hù)明確的拒絕了你的程序?qū)νㄓ嶄浀脑L問(wèn)
- kABAuthorizationStatusAuthorized: 用戶(hù)已經(jīng)授權(quán)給你的程序?qū)νㄓ嶄涍M(jìn)行訪問(wèn)
- 申請(qǐng)通訊錄訪問(wèn)授權(quán)(一般在程序啟動(dòng)時(shí)的application:didFinishLaunchingWithOptions:代理里面寫(xiě))
// 1.獲取授權(quán)的狀態(tài)
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
// 2.判斷授權(quán)狀態(tài),如果是未決定狀態(tài),才需要請(qǐng)求
if (status == kABAuthorizationStatusNotDetermined) {
// 2.1.創(chuàng)建通信錄對(duì)象
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
// 2.2.請(qǐng)求授權(quán)
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
NSLog(@"授權(quán)成功");
} else {
NSLog(@"授權(quán)失敗");
}
});
}
1.使用<AddressBookUI.framework>訪問(wèn)通訊錄
- 1.導(dǎo)入框架
import <AddressBookUI/AddressBookUI.h>
- 2.創(chuàng)建通訊錄界面
// 1.創(chuàng)建選擇聯(lián)系人的界面
ABPeoplePickerNavigationController *ppnc = [[ABPeoplePickerNavigationController alloc] init];
// 2.設(shè)置代理
ppnc.peoplePickerDelegate = self;
// 3.彈出選擇聯(lián)系人界面
[self presentViewController:ppnc animated:YES completion:nil];
// 實(shí)現(xiàn)代理
@interface ViewController<ABPeoplePickerNavigationControllerDelegate>
@end
#pragma mark - 實(shí)現(xiàn)ABPeoplePickerNavigationController的代理方法
// 選中某一個(gè)聯(lián)系人的時(shí)候,會(huì)執(zhí)行該代理方法
// 如果實(shí)現(xiàn)了該方法,那么就不會(huì)進(jìn)入聯(lián)系人的詳細(xì)界面
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
/*
__bridge NSString * : 將CoreFoundation框架的對(duì)象所有權(quán)交給Foundation框架來(lái)使用,但是Foundation框架中的對(duì)象并不能管理該對(duì)象內(nèi)存
__bridge_transfer NSString * : 將CoreFoundation框架的對(duì)象所有權(quán)交給Foundation來(lái)管理,如果Foundation中對(duì)象銷(xiāo)毀,那么我們之前的對(duì)象(CoreFoundation)會(huì)一起銷(xiāo)毀
*/
// 1.獲取選中聯(lián)系人的姓名(姓l(shuí)astname和名firstname)
CFStringRef firstname = ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFStringRef lastname = ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *firstName = (__bridge_transfer NSString *)(firstname);
NSString *lastName = (__bridge_transfer NSString *)(lastname);
NSLog(@"%@ %@", firstName, lastName);
// 2.獲取聯(lián)系人的電話號(hào)碼
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex count = ABMultiValueGetCount(phones);
for (CFIndex i = 0; i < count; i++) {
NSString *phoneLabel = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"label : %@ value : %@", phoneLabel, phoneValue);
}
// 3.釋放不再使用的對(duì)象
CFRelease(phones);
}
// 選擇某一個(gè)聯(lián)系人的某一個(gè)屬性時(shí),會(huì)執(zhí)行該方法
// property選中的屬性
// identifier : 每一個(gè)屬性都由一個(gè)對(duì)應(yīng)標(biāo)示
// 如果實(shí)現(xiàn)了該方法,那么選中一個(gè)聯(lián)系人的屬性時(shí),就會(huì)推出控制器.不會(huì)進(jìn)入下一個(gè)頁(yè)面
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
NSLog(@"選擇了某一個(gè)聯(lián)系人的某一個(gè)屬性");
}
// 點(diǎn)擊了取消按鈕會(huì)執(zhí)行該方法
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
}
2.使用<AddressBook.framework>訪問(wèn)通訊錄
- 1.導(dǎo)入框架
import <AddressBook/AddressBook.h>
- 2.訪問(wèn)通訊錄
// 1.獲取授權(quán)的狀態(tài)
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
// 2.如果用戶(hù)已經(jīng)授權(quán)
if (status != kABAuthorizationStatusAuthorized) return;
// 3.創(chuàng)建通信錄對(duì)象
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
// 4.從通信錄對(duì)象中,將所有的聯(lián)系人拷貝出來(lái)
CFArrayRef peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook);
// 5.遍歷所有的聯(lián)系人(每一個(gè)聯(lián)系人都是一條記錄)
CFIndex peopleCount = CFArrayGetCount(peopleArray);
for (CFIndex i = 0; i < peopleCount; i++) {
// 6.獲取到聯(lián)系人
ABRecordRef person = CFArrayGetValueAtIndex(peopleArray, i);
// 7.獲取姓名
NSString *lastname = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSLog(@"%@ %@", lastname, firstName);
}
// 8.釋放不再使用的對(duì)象
CFRelease(peopleArray);
CFRelease(addressBook);
3.使用第三方框架<RHAddressBook/AddressBook.h>訪問(wèn)通訊錄
#import <RHAddressBook/AddressBook.h>
// 1.獲取授權(quán)狀態(tài)
RHAuthorizationStatus status = [RHAddressBook authorizationStatus];
// 2.判斷授權(quán)狀態(tài),如果未決定狀態(tài),則請(qǐng)求授權(quán)
if (status == RHAuthorizationStatusNotDetermined) {
// 3.創(chuàng)建通信錄對(duì)象
RHAddressBook *addressBook = [[RHAddressBook alloc] init];
// 4.請(qǐng)求授權(quán)
[addressBook requestAuthorizationWithCompletion:^(bool granted, NSError *error) {
if (granted) {
NSLog(@"授權(quán)成功");
} else {
NSLog(@"授權(quán)失敗");
}
}];
}
// 1.獲取授權(quán)狀態(tài)
RHAuthorizationStatus status = [RHAddressBook authorizationStatus];
// 2.判斷如果是未授權(quán),則直接返回
if (status != RHAuthorizationStatusAuthorized) return;
// 3.創(chuàng)建通信錄
RHAddressBook *addressBook = [[RHAddressBook alloc] init];
// 4.獲取所有的聯(lián)系人
NSArray *peopleArray = addressBook.people;
// 5.遍歷所有的聯(lián)系人
for (RHPerson *person in peopleArray) {
// 6.獲取聯(lián)系人的姓名
NSLog(@"%@ %@", person.firstName, person.lastName);
// 7.獲取電話號(hào)碼
RHMultiValue *phones = person.phoneNumbers;
for (int i = 0; i < phones.count; i++) {
// 8.獲取電話號(hào)碼和對(duì)應(yīng)的Label
NSString *phoneLabel = [phones labelAtIndex:i];
NSString *phoneValue = [phones valueAtIndex:i];
NSLog(@"%@ %@", phoneLabel, phoneValue);
}
}