Creating a new ABRecord

2.5k views Asked by At

I am working with ABAddressBook. I have checked out the API docs but could not find any API related to creating a new ABRecord. But in ABAddressBook, a method ABAddressBookAddRecord is available. But I didnt find any APIs available to create a new record. Is there any way to do this?

Best Regards,

Mohammed Sadiq.

1

There are 1 answers

0
Ankit Sachan On
// create new address book person record
ABRecordRef aRecord = ABPersonCreate(); 
CFErrorRef  anError = NULL; 
// adjust record firstname
ABRecordSetValue(aRecord, kABPersonFirstNameProperty, 
                                 CFSTR("Jijo"), &anError); 
// adjust record lastname
ABRecordSetValue(aRecord, kABPersonLastNameProperty, 
                                 CFSTR("Pulikkottil"), &anError); 
if (anError != NULL) { 
    NSLog(@"error while creating..");
}

CFStringRef firstName, lastName; 
firstName = ABRecordCopyValue(aRecord, kABPersonFirstNameProperty); 
lastName  = ABRecordCopyValue(aRecord, kABPersonLastNameProperty); 

ABAddressBookRef addressBook; 
CFErrorRef error = NULL; 
addressBook = ABAddressBookCreate(); 

// try to add new record in the address book
BOOL isAdded = ABAddressBookAddRecord ( addressBook,
                                        aRecord,
                                        &error
);

// check result flag
if(isAdded){
    NSLog(@"added..");
}
// check error flag
if (error != NULL) {
    NSLog(@"ABAddressBookAddRecord %@", error);
} 
error = NULL;

// save changes made in address book
BOOL isSaved = ABAddressBookSave (
                               addressBook,
                               &error
);

// check saved flag
if(isSaved){
    NSLog(@"saved..");
}

// check error flag
if (error != NULL) {
    NSLog(@"ABAddressBookSave %@", error);
} 

CFRelease(aRecord); 
CFRelease(firstName); 
CFRelease(lastName); 
CFRelease(addressBook);