iOS 修改手机号,添加号码到通讯录

类似微信点击某个手机号 弹出识别提示框

引入头文件

#import<AddressBookUI/AddressBookUI.h>
#import <AddressBook/ABAddressBook.h>

需要实现的委托

<ABNewPersonViewControllerDelegate,
ABPersonViewControllerDelegate,
ABPeoplePickerNavigationControllerDelegate>

点击某个号码弹出提示框 由于本文只做了通信录操作呼叫和复制并没有实现

- (void)selectorPhoneButton:(UIButton*)sender
{
    NSString * phone =[NSString stringWithFormat:@"%@:可能是一个电话号码",@"18976373826"];

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil
                                                                   message:phone
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * callAction = [UIAlertAction actionWithTitle:@"呼叫"
                                                style:UIAlertActionStyleDefault
                                             handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:callAction];

    UIAlertAction * copyAction = [UIAlertAction actionWithTitle:@"复制"
                                                style:UIAlertActionStyleDefault
                                                handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:copyAction];

    UIAlertAction * remarkAction = [UIAlertAction actionWithTitle:@"添加备注"
                                                 style:UIAlertActionStyleDefault
                                                handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:remarkAction];
    
    UIAlertAction * addColleagueAction = [UIAlertAction actionWithTitle:@"添加到手机通讯录"
                                                        style:UIAlertActionStyleDefault
                                                        handler:^(UIAlertAction * _Nonnull action) {
                                                            
       [self addColleague];
    }];
    [alert addAction:addColleagueAction];
    
    UIAlertAction * defaultAction = [UIAlertAction actionWithTitle:@"取消"
                                                   style:UIAlertActionStyleCancel
                                                    handler:^(UIAlertAction * action) {
                                                              
                                                    }];
    
    [alert addAction:defaultAction];
    
    
    [self presentViewController:alert animated:YES completion:nil];
}

点击添加到手机通讯录后 再次弹出提示框

- (void)addColleague
{
    NSString * phone = self.editPersonPhone;
    NSString * msg =[NSString stringWithFormat:@"%@:可能是一个电话号码",phone];
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil
                                                                   message:msg
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * callAction = [UIAlertAction actionWithTitle:@"创建新联系人"
                                                          style:UIAlertActionStyleDefault
                                                        handler:^(UIAlertAction * _Nonnull action) {
                                                              [self createNewPerson:phone];
                                                        }];
    [alert addAction:callAction];
    
    UIAlertAction * copyAction = [UIAlertAction actionWithTitle:@"添加到现有联系人"
                                                          style:UIAlertActionStyleDefault
                                                        handler:^(UIAlertAction * _Nonnull action) {
                                                            [self updatePersonPhone:phone];
                                                        }];
    [alert addAction:copyAction];
    
    UIAlertAction * defaultAction = [UIAlertAction actionWithTitle:@"取消"
                                                             style:UIAlertActionStyleCancel
                                                           handler:^(UIAlertAction * action) {
                                                            
                                                           }];
    
    [alert addAction:defaultAction];
    
    [self presentViewController:alert animated:YES completion:nil];
}

 

- (ABRecordRef)addABRecordRef:(NSString*)phone
{
    ABRecordRef newPerson = ABPersonCreate();
    CFErrorRef error = NULL;
    ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFTypeRef)(phone), kABPersonPhoneMainLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,&error);
    CFRelease(multiPhone);
    return newPerson;
}
/** 创建新联系人*/
- (void)createNewPerson:(NSString*)number
{
    //新添加联系人
    ABNewPersonViewController * newPerView = [[ABNewPersonViewController alloc] init];
    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:newPerView];
    newPerView.newPersonViewDelegate = self;
    newPerView.displayedPerson = [self addABRecordRef:number];
    newPerView.title = @"新添加联系人";
    [self presentViewController:nav animated:YES completion:^{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    }];
    
}
//新创建联系人回调
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person
{
    //用于编辑新联系人后做的事件,比如说模态退出,比如说不保存联系人到通讯录
    if (person!=NULL) {
        ABAddressBookRef adbk= ABAddressBookCreateWithOptions(NULL, NULL);
        //从数据库中删除新加的联系人
        ABAddressBookRemoveRecord(adbk, person, NULL);
        CFStringRef name=ABRecordCopyCompositeName(person);
        NSLog(@"name%@",name);//对新加的联系人做处理,不一定要保存到通讯录数据库
        CFRelease(name);
        CFRelease(adbk);
        
    }
    [self dismissViewControllerAnimated:YES completion:^{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        
    }];
}


/** 编辑先有联系人,添加新号码*/
- (void)updatePersonPhone:(NSString*)number{
    
    self.editPersonPhone = number;
    ABPeoplePickerNavigationController * picker =[[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    [self presentViewController:picker animated:YES completion:^{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    }];
    
}

#pragma mark ABPeoplePickerNavigationControllerDelegate

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person
{
    self.selectEditPerson = person;
    [self dismissViewControllerAnimated:YES completion:^{
        //选择联系人后编辑保存
        [self unknownPerson:self.selectEditPerson];
    }];
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    return YES;
}
//编辑联系人
- (void)unknownPerson:(ABRecordRef)person
{
    //获取联系人所有电话
    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
    ABMutableMultiValueRef multi=ABMultiValueCreateMutableCopy(phoneNumbers);
    //添加新的字段
    ABMultiValueAddValueAndLabel(multi,(__bridge CFStringRef)self.editPersonPhone, kABPersonPhoneMobileLabel, nil);
    //修改联系人电话
    ABRecordSetValue(person, kABPersonPhoneProperty, multi, nil);
    
    //显示和编辑一个联系人信息
    ABPersonViewController * personVc=[[ABPersonViewController alloc]init];
    personVc.personViewDelegate = self;
    personVc.displayedPerson    = person;
    CFRelease(person);
    personVc.allowsEditing      = YES;
    personVc.title = @"编辑";
    [personVc setEditing:YES animated:YES];
    
    //把联系人信息作为导航栏的根视图,才可以显示编辑按钮和 完成按钮,返回按钮
    UINavigationController * personNav=[[UINavigationController alloc]initWithRootViewController:personVc];
    personNav.navigationBarHidden = NO;
    UINavigationBar * subNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
    subNavBar.barStyle = UIBarStyleDefault;
    
    UINavigationItem *navItem  = [[UINavigationItem alloc] init];
    UIBarButtonItem *backItem  = [[UIBarButtonItem alloc]
                                  initWithTitle:@"取消"//kValue(@"common_title_cancel")
                                  style:UIBarButtonItemStylePlain
                                  target:self
                                  action:@selector(cancelEdit:)];
    UIBarButtonItem *okItem    = [[UIBarButtonItem alloc]
                                  initWithTitle:@"完成"//kValue(@"common_title_done")
                                  style:UIBarButtonItemStylePlain
                                  target:self
                                  action:@selector(okEdit:)];
    
    navItem.leftBarButtonItem  = backItem;
    navItem.rightBarButtonItem = okItem;
    
    subNavBar.items = [NSArray arrayWithObjects:navItem, nil];
    [personNav.view addSubview:subNavBar];
    
    self.editPersonNavigation = personNav;
    
    [self presentViewController:personNav animated:YES completion:^{
        SEL selAction = [personVc editButtonItem].action;
        [[personNav editButtonItem].target performSelector:selAction withObject:nil afterDelay:0];
    }];
}
//取消通讯录编辑
- (void)cancelEdit:(id)sender
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    [self.editPersonNavigation.topViewController dismissViewControllerAnimated:YES completion:^{
        
    }];
}
//保存修改后通讯录中联系人
- (void)okEdit:(id)sender
{
    //获得电话本
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    //保存电话本
    ABAddressBookAddRecord(addressBook,  (__bridge ABRecordRef)(self.editPersonPhone), nil);
    ABAddressBookSave(addressBook, NULL);
    [self cancelEdit:nil];
    
}

 

 

 

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注