- 一、iOS通讯录添加联系人,是调用到系统页面。
- 导入
#import <AddressBookUI/ABNewPersonViewController.h>
框架。 - 遵守协议
ABNewPersonViewControllerDelegate
代理。 - 设置rightItem:
UIBarButtonItem *rightBBItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Addcontacts:)];
self.navigationItem.rightBarButtonItem = rightBBItem;
- 具体
联系人增加
代码如下:
//iOS9.0以下
ABNewPersonViewController *picker = [[ABNewPersonViewController alloc]init];
picker.newPersonViewDelegate = self;
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:picker];
[self presentViewController:navigation animated:YES completion:^{
//刷新UI
}];
- 设置了代理,实现协议方法
#pragma mark - ABNewPersonViewControllerDelegate
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(nullable ABRecordRef)person{
[newPersonView dismissViewControllerAnimated:YES completion:nil];
}
二、通讯录联系人编辑
自定义的通讯录联系人列表,选中某一行联系人进行编辑。
- 导入
#import <AddressBookUI/ABNewPersonViewController.h>
框架。 - 遵守协议
ABNewPersonViewControllerDelegate
代理。 - 设置rightItem:
//编辑联系人
UIBarButtonItem *rightBBItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(EditClicked:)];
self.navigationItem.rightBarButtonItem = rightBBItem;
- 具体
联系人编辑
的代码如下:
#pragma mark - 编辑联系人
-(void)EditClicked{
ABNewPersonViewController *picker = [[ABNewPersonViewController alloc]init];
ABAddressBookRef adressBook = ABAddressBookCreate();
ABRecordID recordID = _model.recordID; //关键代码1
picker.displayedPerson = ABAddressBookGetPersonWithRecordID(adressBook, recordID); //关键代码2
picker.newPersonViewDelegate = self;
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:picker];
[self presentViewController:navigation animated:YES completion:^{
//刷新UI,
}];
}
5.遵守协议,实现代理方法
#pragma mark - ABNewPersonViewControllerDelegate
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(nullable ABRecordRef)person{
[newPersonView dismissViewControllerAnimated:YES completion:nil];
}
三、 增加联系人和编辑联系人区别:
- 编辑联系人是在增加联系人的基础上做修改的, 增加了下面3行代码,其他的遵守的代理以及实现的协议方法都完全一样.
ABAddressBookRef adressBook = ABAddressBookCreate();
ABRecordID recordID = _model.recordID; //每个联系人的标识
picker.displayedPerson = ABAddressBookGetPersonWithRecordID(adressBook, recordID);
网友评论