iOS开发之AddressBookUI框架详解
一、关于AddressBookUI框架
AddressbookUI是iOS开发框架中提供的一套通讯录界面组件。其中封装好了一套选择联系人,查看联系人的界面,在需要时开发者可以直接调用。当然对于联系人界面,开发者也可以进行完全的自定义,下面链接博客中介绍了如何使用AddressBook框架操作通讯录与联系人。
https://my.oschina.net/u/2340880/blog/1930414
AddressBookUI框架主要提供了如下几个类:
ABNewPersonViewController:新建联系人界面视图控制器
ABPeoplePickerNavigationController:从通讯录选择联系人界面视图控制器
ABPersonViewController:联系人详情界面视图控制器
ABUnknownPersonViewController:一个未在当前通讯录中的联系人查看界面,可以添加和编辑
二、ABNewPersonViewController新建联系人界面
ABNewPersonViewController类的使用非常简单,示例如下:
1 2 3
| ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init]; picker.newPersonViewDelegate = self; [self presentModalViewController:picker animated:YES];
|
效果如下图所示:
ABNewPersonViewController解析如下:
1 2 3 4 5 6 7 8
| @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;
|
联系人的新建回调可以在代理方法中处理,如下:
1 2 3 4
| @protocol ABNewPersonViewControllerDelegate <NSObject>
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(nullable ABRecordRef)person; @end
|
三、ABPeoplePickerNavigationController选择联系人界面
ABPeoplePickerNavigationController是用户通讯录界面,开发者在需要用户选择联系人时,可以直接调用这个界面来让用户进行选择,示例如下:
1 2 3
| ABPeoplePickerNavigationController *vc = [[ABPeoplePickerNavigationController alloc] init]; vc.peoplePickerDelegate = self; [self presentViewController:vc animated:YES completion:nil];
|
效果如下图:
ABPeoplePickerNavigationController解析如下:
1 2 3 4 5 6 7 8 9 10 11 12
| @property(nonatomic,assign,nullable) id<ABPeoplePickerNavigationControllerDelegate> peoplePickerDelegate;
@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;
|
用来进行联系人筛选的属性定义如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| 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中方法解释如下:
1 2 3 4 5 6
| - (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是联系人的详情展示界面,简单使用如下:
1 2 3 4 5 6 7 8 9 10 11
| 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];
|
界面如下:
ABPersonViewController中常用属性方法解析如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @property(nonatomic,assign,nullable) id<ABPersonViewControllerDelegate> personViewDelegate;
@property(nonatomic,readwrite,nullable) ABAddressBookRef addressBook;
@property(nonatomic,readwrite) ABRecordRef displayedPerson;
@property(nonatomic,copy,nullable) NSArray<NSNumber*> *displayedProperties;
@property(nonatomic) BOOL allowsEditing;
@property(nonatomic) BOOL allowsActions;
@property(nonatomic) BOOL shouldShowLinkedPeople;
- (void)setHighlightedItemForProperty:(ABPropertyID)property withIdentifier:(ABMultiValueIdentifier)identifier;
|
ABPersonViewControllerDelegate中方法解释如下:
1 2
| - (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier;
|
五、关于ABUnknownPersonViewController
ABUnknownPersonViewController界面与ABPersonViewController基本一致,不同的是,ABPersonViewController需要使用一个通讯录中已经存在的联系人作为参数进行展示,ABUnknownPersonViewController则不然,你可以使用一个通讯录中不存在的联系人对象来进行界面的渲染,并且支持用户选择将此联系人存入通讯录中。示例如下:
1 2 3 4
| ABUnknownPersonViewController *unknown=[[ABUnknownPersonViewController alloc]init]; unknown.displayedPerson=ABPersonCreate(); unknown.allowsAddingToAddressBook=YES; [self presentViewController:unknown animated:YES completion:nil];
|
ABUnknownPersonViewController中属性方法解释如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @property(nonatomic,assign,nullable) id<ABUnknownPersonViewControllerDelegate> unknownPersonViewDelegate;
@property(nonatomic,readwrite,nullable) ABAddressBookRef addressBook;
@property(nonatomic,readwrite) ABRecordRef displayedPerson;
@property(nonatomic,copy,nullable) NSString *alternateName;
@property(nonatomic,copy,nullable) NSString *message;
@property(nonatomic) BOOL allowsActions;
@property(nonatomic) BOOL allowsAddingToAddressBook;
|
ABUnknownPersonViewControllerDelegate方法:
1 2 3 4
| - (void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownCardViewController didResolveToPerson:(nullable ABRecordRef)person;
- (BOOL)unknownPersonViewController:(ABUnknownPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier;
|