iOS通讯录联系人增加和编辑

  • 一、iOS通讯录添加联系人,是调用到系统页面。

  1. 导入#import 框架。
  2. 遵守协议ABNewPersonViewControllerDelegate代理。
  3. 设置rightItem:
 UIBarButtonItem *rightBBItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Addcontacts:)];
    self.navigationItem.rightBarButtonItem = rightBBItem;
  1. 具体联系人增加代码如下:
//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];
}

二、通讯录联系人编辑
自定义的通讯录联系人列表,选中某一行联系人进行编辑。

  1. 导入#import 框架。
  2. 遵守协议ABNewPersonViewControllerDelegate代理。
  3. 设置rightItem:
 //编辑联系人
    UIBarButtonItem  *rightBBItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(EditClicked:)];
    self.navigationItem.rightBarButtonItem = rightBBItem;
  1. 具体联系人编辑 的代码如下:
#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); 

你可能感兴趣的:(iOS通讯录联系人增加和编辑)