Hope you know how to add a contact to AddressBook,
And have stored the recordid in our application while adding.
following is the piece of code to get the contact id in Addressbook
.....
ABAddressBookAddRecord (addressBook, aRecord, &error);
if(ABAddressBookSave ( addressBook, &error)){
recordid = ABRecordGetRecordID (aRecord);
}
.....
Update a record
inputs are recordid, NSString* firstName,lastName,companyName
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef aRecord = ABAddressBookGetPersonWithRecordID(addressBook, recordid);
if(aRecord){
/*** update firstname, lastname, company name **/
CFErrorRef error = NULL;
if(firstName){
if([firstName length]==0) firstName = nil;
ABRecordSetValue(aRecord, kABPersonFirstNameProperty, firstName, &error);
}
if(lastName){
if([lastName length]==0) lastName = nil;
ABRecordSetValue(aRecord, kABPersonLastNameProperty, lastName, &error);
}
if(companyName!= nil){
if([companyName length]==0) companyName = nil;
ABRecordSetValue(aRecord, kABPersonOrganizationProperty, companyName, &error);
}
/*******************/
CFErrorRef error = NULL;
BOOL isSaved = ABAddressBookSave ( addressBook, &error);
CFRelease(addressBook);
}
And have stored the recordid in our application while adding.
following is the piece of code to get the contact id in Addressbook
.....
ABAddressBookAddRecord (addressBook, aRecord, &error);
if(ABAddressBookSave ( addressBook, &error)){
recordid = ABRecordGetRecordID (aRecord);
}
.....
Update a record
inputs are recordid, NSString* firstName,lastName,companyName
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef aRecord = ABAddressBookGetPersonWithRecordID(addressBook, recordid);
if(aRecord){
/*** update firstname, lastname, company name **/
CFErrorRef error = NULL;
if(firstName){
if([firstName length]==0) firstName = nil;
ABRecordSetValue(aRecord, kABPersonFirstNameProperty, firstName, &error);
}
if(lastName){
if([lastName length]==0) lastName = nil;
ABRecordSetValue(aRecord, kABPersonLastNameProperty, lastName, &error);
}
if(companyName!= nil){
if([companyName length]==0) companyName = nil;
ABRecordSetValue(aRecord, kABPersonOrganizationProperty, companyName, &error);
}
/*******************/
CFErrorRef error = NULL;
BOOL isSaved = ABAddressBookSave ( addressBook, &error);
CFRelease(addressBook);
}
No comments:
Post a Comment