Tuesday, June 12, 2012

Export and import contacts to iPhone contacts database


Export....
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABPersonCreate();
CFErrorRef anError = NULL;
ABRecordSetValue(person, kABPersonFirstNameProperty, @"Don Juan", NULL);
ABAddressBookAddRecord (addressBook,person,nil);
ABAddressBookSave(addressBook, &anError);

Import....
ABAddressBookRef addressBook = ABAddressBookCreate();
int noOfPerson = ABAddressBookGetPersonCount(addressBook);
NSMutableArray *allPeople = (NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
for(int i=0; i < noOfPerson; i++ ){
ABRecordRef person = [allPeople objectAtIndex:i];
if(ABRecordCopyValue(person, kABPersonEmailProperty) != NULL){  // Checks if email for the contact exists

}
NSString *firstName;
if(ABRecordCopyValue(person, kABPersonFirstNameProperty) != NULL){
firstName = [[NSString stringWithFormat:@"%@", ABRecordCopyValue(person, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"%@",firstName);
}

NSString *lastName;
if(ABRecordCopyValue(person, kABPersonLastNameProperty) != NULL){
lastName = [[NSString stringWithFormat:@"%@", ABRecordCopyValue(person, kABPersonLastNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"---%@ ",lastName);
}
}



No comments:

Post a Comment