修改代码:
- (void)update
{
//检查用户是否授权
if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) return;
//1.创建通讯录实例
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
//2.获取通讯录中的所有联系人
NSArray *allPeople = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(book);
ABRecordRef people = (__bridge ABRecordRef)(allPeople[0]);
CFStringRef lastName = (__bridge CFStringRef)@"刘";
//修改姓
ABRecordSetValue(people, kABPersonLastNameProperty, lastName, NULL);
//同步
ABAddressBookSave(book, NULL);
}
添加联系人:
- (void)add
{
//检查用户是否授权
if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) return;
//创建新的联系人
ABRecordRef people = ABPersonCreate();
//设置信息
ABRecordSetValue(people, kABPersonLastNameProperty, (__bridge CFStringRef)@"刘", NULL);
ABRecordSetValue(people, kABPersonFirstNameProperty, (__bridge CFStringRef)@"蛋", NULL);
ABMultiValueRef phone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(phone, (__bridge CFStringRef)@"10086", kABPersonPhoneHomeFAXLabel, NULL);
ABMultiValueAddValueAndLabel(phone, (__bridge CFStringRef)@"10086", kABPersonPhoneWorkFAXLabel, NULL);
ABRecordSetValue(people, kABPersonPhoneProperty, phone , NULL);
//3.添加联系人到通讯录
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookAddRecord(book, people, NULL);
ABAddressBookAddRecord(book, people, NULL);
ABAddressBookSave(book, NULL);
//4.释放
CFRelease(phone);
CFRelease(people);
CFRelease(book);
}