前面学习了iOS通讯录框架之一AddressBook,这里看下AddressBookUI的用法。AddressBookUI框架提供了4个视图控制器和4个对应的委托协议,在前面已经详细说了,这里不再赘述。下面直接看Demo:
ViewController.h
#import <UIKit/UIKit.h>
#import <AddressBookUI/AddressBookUI.h>
@interface ViewController : UITableViewController<ABPeoplePickerNavigationControllerDelegate,ABPersonViewControllerDelegate,ABNewPersonViewControllerDelegate,ABUnknownPersonViewControllerDelegate>
@property (nonatomic,strong) NSMutableArray *contactNameArray;
@property (nonatomic,strong) NSMutableArray *contactIDArray;
- (IBAction)selectAdd:(id)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.contactNameArray = [[NSMutableArray alloc] init];
self.contactIDArray = [[NSMutableArray alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0){
return self.contactIDArray.count;
}else{
return 2;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CELL";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if(indexPath.section == 0){
cell.textLabel.text = [self.contactNameArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}else{
cell.textLabel.textColor = [UIColor blueColor];
cell.accessoryType = UITableViewCellAccessoryNone;
if(indexPath.row == 0){
cell.textLabel.text = @"Create New Contact";
}else{
cell.textLabel.text = @"Add to Existing Contact";
}
}
return cell;
}
#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0){
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, [[self.contactIDArray objectAtIndex:indexPath.row] intValue]);
ABPersonViewController *personViewController = [[ABPersonViewController alloc] init];
personViewController.personViewDelegate = self;
//设置显示的联系人对象
personViewController.displayedPerson = person;
//设置是否显示动作按钮
personViewController.allowsActions = NO;
//设置联系人是否可编辑
personViewController.allowsEditing = YES;
personViewController.displayedProperties = @[[NSNumber numberWithInt:kABPersonPhoneProperty],[NSNumber numberWithInt:kABPersonEmailProperty]];
//ABPersonViewController控制器必须在一个导航控制器内管理
[self.navigationController pushViewController:personViewController animated:YES];
CFRelease(addressBook);
}else{
if(indexPath.row == 0){//create new contact
[self createNewContact];
}else{//add to existing contact
[self addToExistingContact];
}
}
}
#pragma mark - create new contact
-(void)createNewContact
{
//ABNewPersonViewController必须在导航控制器内管理
ABNewPersonViewController *newPersonViewController = [[ABNewPersonViewController alloc] init];
newPersonViewController.newPersonViewDelegate = self;
UINavigationController *newNavigationController = [[UINavigationController alloc]
initWithRootViewController:newPersonViewController];
[self presentViewController:newNavigationController animated:YES completion:nil];
}
#pragma mark - add to existing contact
-(void)addToExistingContact
{
ABRecordRef record = ABPersonCreate();
//name
ABRecordSetValue(record, kABPersonFirstNameProperty, @"jerry", NULL);
//email
ABMutableMultiValueRef multiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiValue, @"[email protected]", kABWorkLabel, NULL);
ABMultiValueAddValueAndLabel(multiValue, @"[email protected]", kABHomeLabel, NULL);
ABRecordSetValue(record, kABPersonEmailProperty, multiValue, NULL);
ABUnknownPersonViewController *unknownPersonViewController = [[ABUnknownPersonViewController alloc] init];
unknownPersonViewController.unknownPersonViewDelegate = self;
unknownPersonViewController.displayedPerson = record;
unknownPersonViewController.allowsAddingToAddressBook = YES;
[self.navigationController pushViewController:unknownPersonViewController animated:YES];
CFRelease(multiValue);
CFRelease(record);
}
#pragma mark - ABPeoplePickerNavigationControllerDelegate
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
//返回YES,选中联系人属性后回调用该属性的默认动作(如,选中Email属性,会调用iOS内置的Email程序发送Email)
return NO;
}
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
//返回YES,显示选中联系人的详细信息
NSString *name = CFBridgingRelease(ABRecordCopyCompositeName(person));
[self.contactNameArray addObject:name];
NSNumber *idNumber = [NSNumber numberWithInt:ABRecordGetRecordID(person)];
[self.contactIDArray addObject:idNumber];
NSArray *indexPaths = @[[NSIndexPath indexPathForRow:self.contactIDArray.count - 1 inSection:0]];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
[self dismissViewControllerAnimated:YES completion:nil];
return NO;
}
#pragma mark - ABPersonViewControllerDelegate
-(BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
//返回YES,选中联系人属性后回调用该属性的默认动作(如,选中Email属性,会调用iOS内置的Email程序发送Email)
return YES;
}
#pragma mark - ABNewPersonViewControllerDelegate
-(void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person
{
[newPersonView dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - ABUnknownPersonViewControllerDelegate
-(void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownCardViewController didResolveToPerson:(ABRecordRef)person
{
//Sent when the user finishes creating a contact or adding the displayed person properties to an existing contact.
[self.navigationController popViewControllerAnimated:YES];
}
-(BOOL)unknownPersonViewController:(ABUnknownPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
//返回YES,选中联系人属性后回调用该属性的默认动作(如,选中Email属性,会调用iOS内置的Email程序发送Email)
return YES;
}
#pragma mark - IBAction
- (IBAction)selectAdd:(id)sender {
ABPeoplePickerNavigationController *peoplePickerController = [[ABPeoplePickerNavigationController alloc] init];
peoplePickerController.peoplePickerDelegate = self;
//设置联系人详细视图中显示的属性列表
// peoplePickerController.displayedProperties = @[[NSNumber numberWithInt:kABPersonPhoneProperty],[NSNumber numberWithInt:kABPersonEmailProperty]];
[self presentViewController:peoplePickerController animated:YES completion:nil];
}
@end
相比AddressBook,AddressBookUI的用法简单很多,上述demo都有注释说明。