内容字号:默认大号超大号

段落设置:段首缩进取消段首缩进

字体设置:切换到微软雅黑切换到宋体

iOS保存联系人到系统通讯录

2018-05-15 16:23 出处:清屏网 人气: 评论(0

1、首先判断当前通讯录是否存在这个联系人

// 判断通讯录是否存在某个联系人
+ (BOOL)isExistContactNameByName:(NSString *)contactName{
    ABAddressBookRef addBook = nil;
    
    __block BOOL isExist = NO;
    CFErrorRef error = nil;
    addBook = ABAddressBookCreateWithOptions(NULL, &error);
    
    dispatch_semaphore_t sema=dispatch_semaphore_create(0);
    // 申请权限
    ABAddressBookRequestAccessWithCompletion(addBook, ^(bool granted, CFErrorRef error) {
        if (granted) {
            CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addBook);
            CFIndex number = ABAddressBookGetPersonCount(addBook);
            for (NSInteger index = 0; index < number; index++) {
                //获取联系人对象的引用
                ABRecordRef  people = CFArrayGetValueAtIndex(allPeople, index);
                //获取当前联系人名字
                NSString*firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));
                
                if ([firstName isEqualToString:contactName]) {
                    isExist = YES;
                }
            }
        }
        dispatch_semaphore_signal(sema);
    });
    //等待信号触发
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    
    return isExist;
}

2、判断是否存在这个联系人且有通讯录权限

- (void)saveYdCallAnswerPhoneToAdrBook{
    NSString *ydContactName = @"亿点连接呼转测试";
    NSString *ydContactNote = @"这是亿点连接的电话测试号码";
    if (![SSAdrBookHelper isExistContactNameByName:ydContactName] &&
        ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        
        [SSAdrBookHelper createAddBookRecordByPhoneArr:self.ydPhoneArray
                                              andTitle:ydContactName
                                               andNote:ydContactNote];
    }
}

3、存入联系人

// 往通讯录添加一条新联系人
+ (void)createAddBookRecordByPhoneArr:(NSArray *)phoneArr andTitle:(NSString *)title andNote:(NSString *)note{
    CFErrorRef error = NULL;
    if (!phoneArr || !title) {return;}
    
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    ABRecordRef newRecord = ABPersonCreate();

    ABRecordSetValue(newRecord, kABPersonFirstNameProperty, (__bridge CFTypeRef)title, &error);
    
    //创建一个多值属性(电话)
    ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);
//    ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)obj, (__bridge CFStringRef)@"亿点连接呼转测试", NULL);
    
    [phoneArr enumerateObjectsUsingBlock:^(NSString *phone, NSUInteger idx, BOOL * _Nonnull stop) {
    // 添加手机号码
        ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)phone, kABPersonPhoneMobileLabel, NULL);
    }];
    
    ABRecordSetValue(newRecord, kABPersonPhoneProperty, multi, &error);
    
    //添加email
//    ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
//    ABMultiValueAddValueAndLabel(multiEmail, (__bridge CFTypeRef)([self.infoDic objectForKey:@"email"]), kABWorkLabel, NULL);
//    ABRecordSetValue(newRecord, kABPersonEmailProperty, multiEmail, &error);
    
    // 添加备注
    ABRecordSetValue(newRecord, kABPersonNoteProperty, (__bridge CFTypeRef)note, &error);
    
    //添加记录到通讯录操作对象
    ABAddressBookAddRecord(addressBook, newRecord, &error);
    
    //保存通讯录操作对象
    ABAddressBookSave(addressBook, &error);

    CFRelease(multi);
    CFRelease(newRecord);
    CFRelease(addressBook);
}

效果如下:

image

分享给小伙伴们:
本文标签: 通讯录联系人iOS

相关文章

发表评论愿您的每句评论,都能给大家的生活添色彩,带来共鸣,带来思索,带来快乐。

CopyRight © 2015-2016 QingPingShan.com , All Rights Reserved.

清屏网 版权所有 豫ICP备15026204号