//
// ViewController.m
// 通讯录Demo
//
// Created by szphsw4 on 2018/1/4.
// Copyright © 2018年 PH. All rights reserved.
//
#import "ViewController.h"
#import <AddressBookUI/ABPeoplePickerNavigationController.h>
#import <AddressBook/ABPerson.h>
#import <AddressBookUI/ABPersonViewController.h>
@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)testForIos9Before {
// 创建联系人选择控制器
ABPeoplePickerNavigationController *pvc = [[ABPeoplePickerNavigationController alloc] init];
pvc.peoplePickerDelegate = self;
//在iOS8之后,需要添加nav.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];这一段代码,否则选择联系人之后会直接dismiss,不能进入详情选择电话。
pvc.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined){
ABAddressBookRef bookRef = ABAddressBookCreate();
ABAddressBookRequestAccessWithCompletion(bookRef, ^(bool granted, CFErrorRef error) {
if (granted){
NSLog(@"授权成功!");
[self presentViewController:pvc animated:YES completion:nil];
}else{
NSLog(@"授权失败!");
}
});
} else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){
[self presentViewController:pvc animated:YES completion:nil];
}
}
//// 选择某个联系人时调用
//- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person {
// NSLog(@"选中联系人");
// CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
// CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
// NSString *fir = CFBridgingRelease(firstName);
// NSString *las = CFBridgingRelease(lastName);
// NSLog(@"%@---%@", fir, las);
// ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
// CFIndex count = ABMultiValueGetCount(multi);
// for (int i = 0; i < count; i++)
// {
// NSString *label = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(multi, i);
// NSString *phone =(__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(multi, i);
// NSLog(@"%@---%@", label, phone);
// }
//}
////点击联系人
//- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person {
// ABPersonViewController *personViewController = [[ABPersonViewController alloc] init];
// personViewController.displayedPerson = person;
// [peoplePicker pushViewController:personViewController animated:YES];
//}
//取消选择
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
NSLog(@"取消选择");
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
}
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
long index = ABMultiValueGetIndexForIdentifier(phone,identifier);
NSString *phoneNO = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phone, index);
if ([phoneNO hasPrefix:@"+"]) {
phoneNO = [phoneNO substringFromIndex:3];
}
phoneNO = [phoneNO stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"___%@", phoneNO);
}
@end
教学代码
//
// ViewController.m
// 01-通信录(有UI界面)
//
// Created by apple on 15-1-23.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "ViewController.h"
#import <AddressBookUI/AddressBookUI.h>
@interface ViewController () <ABPeoplePickerNavigationControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.创建ABPeoplePickerNavigationController控制器
ABPeoplePickerNavigationController *ppnc = [[ABPeoplePickerNavigationController alloc] init];
// 2.设置代理
ppnc.peoplePickerDelegate = self;
// 3.弹出控制器
[self presentViewController:ppnc animated:YES completion:nil];
}
#pragma mark - ABPeoplePickerNavigationController的代理方法
// =================================iOS8=================================
/**
* 选中某一个联系人的时候会调用该方法
*
* @param person 选中的联系人
*/
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
// 1.取出联系人的姓名
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSLog(@"firstName:%@ lastName:%@", firstName, lastName);
// 2.取出联系人的电话
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex phoneCount = ABMultiValueGetCount(phones);
for (int i = 0; i < phoneCount; i++) {
NSString *phoneLabel = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"%@--%@", phoneLabel, phoneValue);
}
// 3.释放不需要的对象
CFRelease(phones);
}
/**
* 选中某一个联系人的某一个属性的时候会调用该方法
*
* @param person 选中的联系人
* @param property 选中的属性
* @param identifier 属性对应的标识符
*/
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
}
// =================================iOS7=================================
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
// 退出控制器
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
return NO;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
return YES;
}
/**
* 当点击取消按钮的时候会调用该方法
*/
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
}
@end
无界面
//
// ViewController.m
// 02-通信录(无界面)
//
// Created by apple on 15-1-23.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "ViewController.h"
#import <AddressBook/AddressBook.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.创建一个通信录对象
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
// 2.获取通信录中所有联系人
CFArrayRef peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex peopleCount = CFArrayGetCount(peopleArray);
for (int i = 0; i < peopleCount; i++) {
// 3.获取到联系人
ABRecordRef person = CFArrayGetValueAtIndex(peopleArray, i);
// 4.获取联系人的姓名
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSLog(@"%@ -- %@", firstName, lastName);
// 5.获取联系人的电话信息
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex phoneCount = ABMultiValueGetCount(phones);
for (CFIndex i = 0; i < phoneCount; i++) {
NSString *phoneLabel = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"%@--%@", phoneLabel, phoneValue);
}
// 6.释放不需要的对象
CFRelease(phones);
}
// 7.释放不需要的对象
CFRelease(addressBook);
CFRelease(peopleArray);
}
@end