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

作者: czj_warrior | 来源:发表于2017-11-13 16:33 被阅读184次

    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保存联系人到系统通讯录

        本文链接:https://www.haomeiwen.com/subject/qcbzmxtx.html