一、关于AddressBookUI框架
AddressbookUI是iOS开发框架中提供的一套通讯录界面组件。其中封装好了一套选择联系人,查看联系人的界面,在需要时开发者可以直接调用。当然对于联系人界面,开发者也可以进行完全的自定义,下面链接博客中介绍了如何使用AddressBook框架操作通讯录与联系人。
https://my.oschina.net/u/2340880/blog/1930414
AddressBookUI框架主要提供了如下几个类:
ABNewPersonViewController:新建联系人界面视图控制器
ABPeoplePickerNavigationController:从通讯录选择联系人界面视图控制器
ABPersonViewController:联系人详情界面视图控制器
ABUnknownPersonViewController:一个未在当前通讯录中的联系人查看界面,可以添加和编辑
二、ABNewPersonViewController新建联系人界面
ABNewPersonViewController类的使用非常简单,示例如下:
ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
picker.newPersonViewDelegate = self;
[self presentModalViewController:picker animated:YES];
效果如下图所示:
ABNewPersonViewController解析如下:
//代理
@property(nonatomic,assign,nullable) id<ABNewPersonViewControllerDelegate> newPersonViewDelegate;
//通讯录实例 只读
@property(nonatomic,readwrite,nullable) ABAddressBookRef addressBook;
//联系人 只读
@property(nonatomic,readwrite,nullable) ABRecordRef displayedPerson;
//联系人组 只读
@property(nonatomic,readwrite,nullable) ABRecordRef parentGroup;
联系人的新建回调可以在代理方法中处理,如下:
@protocol ABNewPersonViewControllerDelegate <NSObject>
//新建联系人完成后的回调
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(nullable ABRecordRef)person;
@end
三、ABPeoplePickerNavigationController选择联系人界面
ABPeoplePickerNavigationController是用户通讯录界面,开发者在需要用户选择联系人时,可以直接调用这个界面来让用户进行选择,示例如下:
ABPeoplePickerNavigationController *vc = [[ABPeoplePickerNavigationController alloc] init];
vc.peoplePickerDelegate = self;
[self presentViewController:vc animated:YES completion:nil];
效果如下图:
ABPeoplePickerNavigationController解析如下:
//代理
@property(nonatomic,assign,nullable) id<ABPeoplePickerNavigationControllerDelegate> peoplePickerDelegate;
//需要展示的用户联系人属性字段 数组中为属性的ID 在AddressBook框架介绍的博客中有讲解
@property(nonatomic,copy,nullable) NSArray<NSNumber*> *displayedProperties;
//通讯录实例
@property(nonatomic,readwrite,nullable) ABAddressBookRef addressBook;
//设置一个筛选条件 过滤掉不可显示的联系人
@property(nonatomic,copy,nullable) NSPredicate *predicateForEnablingPerson;
//设置一个筛选条件 过滤掉不可选择的联系人
@property(nonatomic,copy,nullable) NSPredicate *predicateForSelectionOfPerson;
//设置一个筛选条件 过滤掉不可显示的属性
@property(nonatomic,copy,nullable) NSPredicate *predicateForSelectionOfProperty;
用来进行联系人筛选的属性定义如下:
extern NSString * const ABPersonNamePrefixProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonGivenNameProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonMiddleNameProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonFamilyNameProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonNameSuffixProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonPreviousFamilyNameProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonNicknameProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonPhoneticGivenNameProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonPhoneticMiddleNameProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonPhoneticFamilyNameProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonOrganizationNameProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonDepartmentNameProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonJobTitleProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonBirthdayProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonNoteProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonPhoneNumbersProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonEmailAddressesProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonUrlAddressesProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonDatesProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonInstantMessageAddressesProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonRelatedNamesProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonSocialProfilesProperty NS_AVAILABLE_IOS(8_0);
extern NSString * const ABPersonPostalAddressesProperty NS_AVAILABLE_IOS(8_0);
ABPeoplePickerNavigationControllerDelegate中方法解释如下:
//选中联系人进行回调
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person;
//选择联系人属性
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier;
//取消选择
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker;
四、ABPersonViewController联系人详情界面
ABPersonViewController是联系人的详情展示界面,简单使用如下:
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
CFArrayRef peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook);
ABRecordRef person = CFArrayGetValueAtIndex(peopleArray, 0);
ABPersonViewController *viewController = [[ABPersonViewController alloc] init];
viewController.personViewDelegate = self;
viewController.displayedPerson = person;
viewController.allowsActions = NO;
viewController.allowsEditing = YES;
viewController.displayedProperties = @[[NSNumber numberWithInt:kABPersonPhoneProperty]];
[self presentViewController:viewController animated:YES completion:nil];
界面如下: