I am using the following code to create a V-card representation for all my contacts.
ABAddressBookRef addressBook = ABAddressBookCreate();
//------------------------------------------------- create vcf file------------------------------------------
CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFDataRef vcards = (CFDataRef)ABPersonCreateVCardRepresentationWithPeople(contacts);
NSString *vcardString = [[NSString alloc] initWithData:(NSData *)vcards encoding:NSUTF8StringEncoding];
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *folderPath = [paths objectAtIndex:0];
NSString *filePath = [folderPath stringByAppendingPathComponent:@"contacts.vcf"];
[vcardString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"Documents directory: %@",[fileMgr contentsOfDirectoryAtPath: folderPath error:&error]);
//------------------------------------------------- create vcf file------------------------------------------
After this I added the contacts to my addressbook by using the following code:
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (exists)
{
NSLog(@"File Exist and Ready to send");
NSString *vCardString = [[NSString alloc]initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
CFDataRef vCardData = (CFDataRef)[vCardString dataUsingEncoding:NSUTF8StringEncoding];
CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, vCardData);
for (CFIndex index = 0; index < CFArrayGetCount(vCardPeople); index++) {
ABRecordRef person = CFArrayGetValueAtIndex(vCardPeople, index);
ABAddressBookAddRecord(addressBook, person, NULL);
CFRelease(person);
}
CFRelease(vCardPeople);
ABAddressBookSave(addressBook, NULL);
}
The issue is when i am adding the records to the addressbook it is not replacing the duplicate contacts. It just adds all the contacts and the almost all the contact becomes duplicate. How can i prevent this to add duplicates. is there any method or some other suggestion which can help. I think before adding the contact to the addressbook we need to check if the same contact already there in addressbook or not. But how can we check if the contact is already there in addressbook or not.
Thanks in advance.