我正在尝试修改联系人数据,但我正在寻找具体案例。是否可以修改联系人数据中的生日条目?如果是这样,我该怎么办呢?我一直无法找到任何东西......
答案 0 :(得分:0)
您可以按如下方式设置联系人的生日:
CFDateRef birthdayRef = (__bridge CFDateRef)birthday; // birthday is a NSDate
ABRecordSetValue(person, kABPersonBirthdayProperty, birthdayRef, error);
其中person
是ABRecordRef
的实例。
例如,要设置其电子邮件地址与名为targetEmailAddress
的变量匹配的联系人的生日:
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error)
{
if (granted) {
CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);
for(int i = 0; i < numberOfPeople; i++) {
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
// Find the person with an email address matching targetEmailAddress
ABMultiValueRef emailAddresses = ABRecordCopyValue(person, kABPersonEmailProperty);
for (CFIndex i = 0; i < ABMultiValueGetCount(emailAddresses); i++) {
NSString *emailAddress = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(emailAddresses, i);
if ([emailAddress isEqualToString:targetEmailAddress]) {
// Set the person's birthday
CFDateRef birthdayRef = (__bridge CFDateRef)birthday; // birthday is a NSDate
ABRecordSetValue(person, kABPersonBirthdayProperty, birthdayRef, error);
ABAddressBookSave(addressBook, error);
// Check for error here
}
}
}
}
});
答案 1 :(得分:0)
简单跟着这个,
//创建地址簿实例
ABAddressBookRef allPeople = ABAddressBookCreate();
//获取具有索引的personn实例 //找到特定人的案例
ABRecordRef person = ABAddressBookGetPersonWithRecordID(allPeople, rid);
//复制人物
ABRecordRef personCopy = person;
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd.MM"];// your b'date format
//don't set year in address book (yyyy=1604)
NSDate *bdate=[formatter dateFromString:[formatter stringFromDate:@"10.12"]]; // 10.12 is your b'date.
ABRecordSetValue(personCopy, kABPersonBirthdayProperty,(__bridge CFDateRef)bdate,nil);
CFErrorRef error;
BOOL remove = ABAddressBookRemoveRecord (
allPeople,
person,
&error);
if(remove) {
BOOL add = ABAddressBookAddRecord (
allPeople,
personCopy,
&error);
BOOL save = ABAddressBookSave(allPeople, &error);
}