作品鏈接:
http://www.reibang.com/users/1e0f5e6f73f6/top_articles
1.iOS9之前有UI
1.導入第三方的框架
#import <AddressBookUI/AddressBookUI.h>
2.代碼實現(xiàn)
#pragma mark - 點擊事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 1.創(chuàng)建選擇聯(lián)系人的控制器
ABPeoplePickerNavigationController *ppnc = [[ABPeoplePickerNavigationController alloc] init];
// 2.設(shè)置代理
ppnc.peoplePickerDelegate = self;
// 3.彈出控制器
[self presentViewController:ppnc animated:YES completion:nil];
}
#pragma mark - <ABPeoplePickerNavigationControllerDelegate>
// 當用戶選中某一個聯(lián)系人時會執(zhí)行該方法,并且選中聯(lián)系人后會直接退出控制器
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
// 1.獲取選中聯(lián)系人的姓名
CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
// (__bridge NSString *) : 將對象交給Foundation框架的引用來使用,但是內(nèi)存不交給它來管理
// (__bridge_transfer NSString *) : 將對象所有權(quán)直接交給Foundation框架的應(yīng)用,并且內(nèi)存也交給它來管理
NSString *lastname = (__bridge_transfer NSString *)(lastName);
NSString *firstname = (__bridge_transfer NSString *)(firstName);
NSLog(@"%@ %@", lastname, firstname);
// 2.獲取選中聯(lián)系人的電話號碼
// 2.1.獲取所有的電話號碼
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex phoneCount = ABMultiValueGetCount(phones);
// 2.2.遍歷拿到每一個電話號碼
for (int i = 0; i < phoneCount; i++) {
// 2.2.1.獲取電話對應(yīng)的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);
}
注意:導入的第三方框架是C語言,使用時防止內(nèi)存泄漏 需要release
2.iOS9之前沒有UI
1.導入第三方框架
#import <AddressBook/AddressBook.h>
2.在啟動app時對用戶請求授權(quán)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/*
kABAuthorizationStatusNotDetermined = 0, 未決定
kABAuthorizationStatusRestricted, 由于系統(tǒng)原因,不允許獲取用戶的通信錄
kABAuthorizationStatusDenied, 拒絕
kABAuthorizationStatusAuthorized 已經(jīng)授權(quán)
*/
// 1.獲取授權(quán)狀態(tài)
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
// 2.如果是未決定狀態(tài),則請求授權(quán)
if (status == kABAuthorizationStatusNotDetermined) {
// 2.1.創(chuàng)建通訊錄對象
ABAddressBookRef addressBook = ABAddressBookCreate();
// 2.2.請求用戶的授權(quán)
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (error) {
NSLog(@"%@", error);
}
if (granted) {
NSLog(@"授權(quán)成功");
} else {
NSLog(@"授權(quán)失敗");
}
});
}
return YES;
}
3.代碼實現(xiàn)
#pragma mark - 點擊事件
- (IBAction)open:(id)sender {
// 1.獲取授權(quán)狀態(tài)
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
// 2.如果已經(jīng)授權(quán),才能獲取聯(lián)系人
if (status != kABAuthorizationStatusAuthorized) return;
// 3.創(chuàng)建通訊錄對象
ABAddressBookRef addressBook = ABAddressBookCreate();
// 4.獲取所有的聯(lián)系人
CFArrayRef peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex peopleCount = CFArrayGetCount(peopleArray);
// 5.遍歷所有的聯(lián)系人
for (int i = 0; i < peopleCount; ++i) {
// 5.1獲取某一個聯(lián)系人
ABMultiValueRef person = CFArrayGetValueAtIndex(peopleArray, i);
// 5.2獲取聯(lián)系人的姓名
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.獲取電話對應(yīng)的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);
}
注意:導入的第三方框架是C語言咕晋,使用時防止內(nèi)存泄漏 需要
3.iOS9之后有UI
1.導入第三方框架
#import <ContactsUI/ContactsUI.h>
2.代碼實現(xiàn)
#pragma mark - 點擊事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 1.創(chuàng)建選擇聯(lián)系人的控制器
CNContactPickerViewController *contactVc = [[CNContactPickerViewController alloc] init];
// 2.設(shè)置代理
contactVc.delegate = self;
// 3.彈出控制器
[self presentViewController:contactVc animated:YES completion:nil];
}
#pragma mark - <CNContactPickerDelegate>
// 當選中某一個聯(lián)系人時會執(zhí)行該方法
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact
{
// 1.獲取聯(lián)系人的姓名
NSString *lastname = contact.familyName;
NSString *firstname = contact.givenName;
NSLog(@"%@ %@", lastname, firstname);
// 2.獲取聯(lián)系人的電話號碼
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);
}
}
4.iOS9之后無UI
1.導入第三方框架
#import <Contacts/Contacts.h>
2.啟動app時請求用戶授權(quán)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 1.獲取授權(quán)狀態(tài)
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
// 2.如果是未決定狀態(tài),則請求授權(quán)
if (status == CNAuthorizationStatusNotDetermined) {
// 2.1.創(chuàng)建通信錄對象
CNContactStore *contactStore = [[CNContactStore alloc] init];
// 2.2.請求授權(quán)
[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error);
}
if (granted) {
NSLog(@"授權(quán)成功");
} else {
NSLog(@"授權(quán)失敗");
}
}];
}
return YES;
}
@end
3.代碼實現(xiàn)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 1.獲取授權(quán)狀態(tài)
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
// 2.判斷授權(quán)狀態(tài),如果不是已經(jīng)授權(quán),則直接返回
if (status != CNAuthorizationStatusAuthorized) return;
// 3.創(chuàng)建通信錄對象
CNContactStore *contactStore = [[CNContactStore alloc] init];
// 4.創(chuàng)建獲取通信錄的請求對象
// 4.1.拿到所有打算獲取的屬性對應(yīng)的key
NSArray *keys = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
// 4.2.創(chuàng)建CNContactFetchRequest對象
CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
// 5.遍歷所有的聯(lián)系人
[contactStore enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
// 1.獲取聯(lián)系人的姓名
NSString *lastname = contact.familyName;
NSString *firstname = contact.givenName;
NSLog(@"%@ %@", lastname, firstname);
// 2.獲取聯(lián)系人的電話號碼
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);
}
}];
}