iOS 往来--书面资料

写接触知识和查询功能的基础,现在我们就来看看信息写入

  • 变化
  • 删除
#pragma mark  - 系人信息

//创建联系人
- (void) creatNewRecord
{
    CFErrorRef error = NULL;
    
    //创建一个通讯录操作对象
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    
    //创建一条新的联系人纪录
    ABRecordRef newRecord = ABPersonCreate();
    
    //为新联系人记录加入属性值
    ABRecordSetValue(newRecord, kABPersonFirstNameProperty, (__bridge CFTypeRef)@"雪松", &error);
    
    //创建一个多值属性
    ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)@"12345678990", kABPersonPhoneMobileLabel, NULL);
    ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)@"11234567890", kABPersonPhoneIPhoneLabel, NULL);
    
    //将多值属性加入到记录
    ABRecordSetValue(newRecord, kABPersonPhoneProperty, multi, &error);
    CFRelease(multi);
    
    //加入记录到通讯录操作对象
    ABAddressBookAddRecord(addressBook, newRecord, &error);
    
    //保存通讯录操作对象
    ABAddressBookSave(addressBook, &error);
    CFRelease(newRecord);
    CFRelease(addressBook);
}

- (void) editRecord
{
    CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    ABRecordID recordID = [personIDASNumber intValue];
    ABRecordRef record = ABAddressBookGetPersonWithRecordID(addressBook, recordID);
    
    //设置电话号码
    ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)@"12345678900", kABPersonPhoneMobileLabel, NULL);
    ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)@"12345678900", kABPersonPhoneIPhoneLabel, NULL);
    
    //加入电话号码到数据库
    ABRecordSetValue(record, kABPersonPhoneProperty, multi, &error);
    CFRelease(multi);
    
    //保存到数据库
    ABAddressBookSave(addressBook, &error);
    CFRelease(addressBook);
    
}

- (void)deleteRecord
{
    CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    ABRecordID recordID = [personIDASNumber intValue];
    ABRecordRef record = ABAddressBookGetPersonWithRecordID(addressBook, recordID);
    
    //删除记录
    ABAddressBookRemoveRecord(addressBook, record, &error);
    
    //保存到数据库
    ABAddressBookSave(addressBook, &error);
    CFRelease(addressBook);
}


版权声明:本文博客原创文章,博客,未经同意,不得转载。

你可能感兴趣的:(ios)