AddressBookUI.framework
、AddressBook.framework
这2个框架在iOS2.0引入。
AddressBookUI:
#import //显示整个通讯录并可以选择一个联系人的信息
#import //显示一个具体联系人的信息
#import //增加一个新的联系人
#import
#import
ABPeoplePickerNavigationController
这个类自带导航条,使用presentViewController
从底部推出一个带导航条的联系人界面;实现ABPeoplePickerNavigationControllerDelegate
代理方法.
ABPeoplePickerNavigationController *vc = [[ABPeoplePickerNavigationController alloc] init];
vc.peoplePickerDelegate = self;
[self presentViewController:vc animated:YES completion:nil];
ABPersonViewController
查看一个联系人的详细信息:
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, personID);
ABPersonViewController *viewController = [[ABPersonViewController alloc] init];
viewController.personViewDelegate = self;
viewController.displayedPerson = person;
viewController.allowsActions = NO;
viewController.allowsEditing = YES;
viewController.displayedProperties = @[[NSNumber numberWithInt:kABPersonPhoneProperty]];
[self.navigationController pushViewController:viewController animated:YES];
CFRelease(addressBook);
ABNewPersonViewController
添加新联系人
ABNewPersonViewController *picker = [[ABNewPersonViewControlleralloc] init];
picker.newPersonViewDelegate = self;
UINavigationController *navigation = [[UINavigationControlleralloc] initWithRootViewController:picker];
[selfpresentModalViewController:navigation animated:YES];
iOS8之前有这样三个方法:
1.- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
,用户点击联系人界面的右上角取消按钮时触发;
2.- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person NS_DEPRECATED_IOS(2_0, 8_0)
,
3.- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_DEPRECATED_IOS(2_0, 8_0)
,
ios8之后新增2个方法,弃用2个方法:
4.- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person NS_AVAILABLE_IOS(8_0)
,该方法会在选中某个联系人之后触发,一旦实现了这个代理方法用户只能选择到联系人视图,无法查看具体联系人的信息;// Called after a person has been selected by the user.
5.- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0)
// Called after a property has been selected by the user.
第一个方法是选中这个人之后调用。
第二个方法是选中这个人的详细信息后调用。
AddressBook:
首先创建一个ABAddressBookRef类的通讯录对象
//创建通讯录对象
self.addressBook=ABAddressBookCreateWithOptions(NULL, NULL);
//请求访问用户通讯录,注意无论成功与否block都会调用
ABAddressBookRequestAccessWithCompletion(self.addressBook, ^(bool granted, CFErrorRef error) {
if (!granted) {
NSLog(@"未获得通讯录访问权限!");
}
[self initAllPerson];
});
获取通讯录中所有人
/**
* 取得所有通讯录记录
*/
-(void)initAllPerson{
//取得通讯录访问授权
ABAuthorizationStatus authorization= ABAddressBookGetAuthorizationStatus();
//如果未获得授权
if (authorization!=kABAuthorizationStatusAuthorized) {
NSLog(@"尚未获得通讯录访问授权!");
return ;
}
//取得通讯录中所有人员记录
CFArrayRef allPeople= ABAddressBookCopyArrayOfAllPeople(self.addressBook);
self.allPerson=(__bridge NSMutableArray *)allPeople;
//释放资源
CFRelease(allPeople);
获取通讯录中所有人员信息
//获取所有联系人的数组
CFArrayRef allLinkPeople = ABAddressBookCopyArrayOfAllPeople(addBook);
//获取联系人总数
CFIndex number = ABAddressBookGetPersonCount(addBook);
//进行遍历
for (NSInteger i=0; i
新建一个联系人
//创建一个联系人引用
ABRecordRef person = ABPersonCreate();
NSString *firstName = @"哈";
NSString *lastName = @"哈";
// 电话号码数组
NSArray *phones = [NSArray arrayWithObjects:@"123",@"456",nil];
// 电话号码对应的名称
NSArray *labels = [NSArray arrayWithObjects:@"iphone",@"home",nil];
//这里的字段和上面的字段完全相同
// 设置名字属性
ABRecordSetValue(person, kABPersonFirstNameProperty,(__bridge CFStringRef)firstName, NULL);
// 设置姓氏属性
ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFStringRef)lastName, NULL);
// 设置生日属性
ABRecordSetValue(person, kABPersonBirthdayProperty,(__bridge CFDateRef)birthday, NULL);
// 字典引用
ABMultiValueRef dic =ABMultiValueCreateMutable(kABMultiStringPropertyType);
// 添加电话号码与其对应的名称内容
for (int i = 0; i < [phones count]; i ++) {
ABMultiValueIdentifier obj = ABMultiValueAddValueAndLabel(dic,(__bridge CFStringRef)[phones objectAtIndex:i], (__bridge CFStringRef)[labels objectAtIndex:i], &obj);
}
// 设置phone属性
ABRecordSetValue(person, kABPersonPhoneProperty, dic, NULL);
// 将新建的联系人添加到通讯录中
ABAddressBookAddRecord(addBook, person, NULL);
// 保存通讯录数据
ABAddressBookSave(addBook, NULL);
修改联系人信息
修改联系人的操作就是将获取和添加和在一起,先获取到相应的联系人引用,重设其属性字段即可。
删除联系人
//获取所有联系人
NSArray *array = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(addBook);
// 遍历所有的联系人
for (id obj in array) {
ABRecordRef people = (__bridge ABRecordRef)obj;
NSString *firstName = (__bridge NSString*)ABRecordCopyValue(people, kABPersonFirstNameProperty);
NSString *lastName = (__bridge NSString*)ABRecordCopyValue(people, kABPersonLastNameProperty);
if ([firstName isEqualToString:@"哈"] &&[lastName isEqualToString:@"哈"]) {
ABAddressBookRemoveRecord(addBook, people,NULL);
}
}
// 保存修改的通讯录对象
ABAddressBookSave(addBook, NULL);
内存管理
-(void)dealloc{
if (self.addressBook != NULL) {
CFRelease(self.addressBook);
}
}
在我们用ABAddressBookCreate()创建一个引用对象时,切记无论ARC还MRC,要用CFRelease()进行释放引用,例如上面的例子,我们需要加上这句代码
CFRelease(self.addressBook);
contactsUI
iOS9中,系统也为我们封装好了一套联系人的UI界面,用起来也十分方便,主要新增的controller有两个:
CNContactPickerViewController
:展示联系人列表的controller
CNContactViewController
:展示联系人详细信息的controller
示例如下:
弹出联系人列表:
CNContactPickerViewController * con = [[CNContactPickerViewController alloc]init];
[self presentViewController:con animated:YES completion:nil];
CNContactPickerDelegate
代理方法:
//视图取消时 调用的方法
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker;
//选中与取消选中时调用的方法
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact;
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty;
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray *)contacts;
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperties:(NSArray *)contactProperties;
CNContactViewController
则是用来显示具体联系人的详细信息的
CNContactViewController * con = [CNContactViewController viewControllerForContact:contact];
[self presentViewController:con animated:YES completion:nil];
//将要展示联系人信息与已经展示联系人信息的回调
- (BOOL)contactViewController:(CNContactViewController *)viewController shouldPerformDefaultActionForContactProperty:(CNContactProperty *)property;
- (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(nullable CNContact *)contact;