1. 程式人生 > >iOS開發之AddressBookUI框架詳解

iOS開發之AddressBookUI框架詳解

iOS開發之AddressBookUI框架詳解

一、關於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];

介面如下:

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

     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;