创建一个新的ABRecord

时间:2009-12-16 06:09:24

标签: iphone abaddressbook abrecord

我正在使用ABAddressBook。我已经检查了API文档但找不到任何文档 与创建新ABRecord相关的API。但在ABAddressBook中,方法ABAddressBookAddRecord可用。但我没有找到任何可用于创建新记录的API。有没有办法做到这一点?

最诚挚的问候,

Mohammed Sadiq。

1 个答案:

答案 0 :(得分:4)

// 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);