views:

297

answers:

1

Hi all,

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.

A: 

ABRecordRef aRecord = ABPersonCreate(); CFErrorRef anError = NULL; ABRecordSetValue(aRecord, kABPersonFirstNameProperty, CFSTR("Jijo"), &anError); 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(); 

BOOL isAdded = ABAddressBookAddRecord (
                                            addressBook,
                                            aRecord,
                                             &error
);

if(isAdded){

    NSLog(@"added..");
}
if (error != NULL) {
    NSLog(@"ABAddressBookAddRecord %@", error);
} 
error = NULL;

BOOL isSaved = ABAddressBookSave (
                               addressBook,
                               &error
);

if(isSaved){

    NSLog(@"saved..");
}

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

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