AddressbookUI是iOS開發框架中提供的一套通信錄界面組件。其中封裝好了一套選擇聯繫人,查看聯繫人的界面,在須要時開發者能夠直接調用。固然對於聯繫人界面,開發者也能夠進行徹底的自定義,下面連接博客中介紹瞭如何使用AddressBook框架操做通信錄與聯繫人。數組
http://www.javashuo.com/article/p-uanrwxqc-ce.html框架
AddressBookUI框架主要提供了以下幾個類:ide
ABNewPersonViewController:新建聯繫人界面視圖控制器atom
ABPeoplePickerNavigationController:從通信錄選擇聯繫人界面視圖控制器spa
ABPersonViewController:聯繫人詳情界面視圖控制器.net
ABUnknownPersonViewController:一個未在當前通信錄中的聯繫人查看界面,能夠添加和編輯代理
ABNewPersonViewController類的使用很是簡單,示例以下:code
ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init]; picker.newPersonViewDelegate = self; [self presentModalViewController:picker animated:YES];
效果以下圖所示:orm
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 *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是聯繫人的詳情展現界面,簡單使用以下:
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中經常使用屬性方法解析以下:
//代理 @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中方法解釋以下:
//選擇屬性發送時調用 - (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier;
ABUnknownPersonViewController界面與ABPersonViewController基本一致,不一樣的是,ABPersonViewController須要使用一個通信錄中已經存在的聯繫人做爲參數進行展現,ABUnknownPersonViewController則否則,你可使用一個通信錄中不存在的聯繫人對象來進行界面的渲染,而且支持用戶選擇將此聯繫人存入通信錄中。示例以下:
ABUnknownPersonViewController *unknown=[[ABUnknownPersonViewController alloc]init]; unknown.displayedPerson=ABPersonCreate(); unknown.allowsAddingToAddressBook=YES;//容許添加 [self presentViewController:unknown animated:YES completion:nil];
ABUnknownPersonViewController中屬性方法解釋以下:
//代理 @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方法:
//聯繫人解釋時調用 - (void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownCardViewController didResolveToPerson:(nullable ABRecordRef)person; //發送活動 - (BOOL)unknownPersonViewController:(ABUnknownPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier;