I'm trying to modify contact data, but I'm looking for a specific case. Is it possible to modify the birthday entry in a contact's data? If so, how can I go about this? I haven't been able to find anything on it...
Asked
Active
Viewed 179 times
2 Answers
0
You can set the birthday of a contact as follows:
CFDateRef birthdayRef = (__bridge CFDateRef)birthday; // birthday is a NSDate
ABRecordSetValue(person, kABPersonBirthdayProperty, birthdayRef, error);
where person
is an instance of ABRecordRef
.
For example, to set the birthday of a contact whose email address matches a variable called 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
}
}
}
}
});

neilco
- 7,964
- 2
- 36
- 41
0
Simple Follow this,
- get the object of ABRecordRef (say person)from addressbook
- remove aRecord from the address book
- set the (changed ) values to aRecord
- add record
- save addressbook
//Create an instance of address book
ABAddressBookRef allPeople = ABAddressBookCreate();
//Get an instance of a personn with its index // find you case with particular person
ABRecordRef person = ABAddressBookGetPersonWithRecordID(allPeople, rid);
//Copy the personn
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);
}

Toseef Khilji
- 17,192
- 12
- 80
- 121