0

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.

  • If you use the right data structure this problem will go away. Try to use HashMap, which does not allow duplicates. – AAV Jul 03 '13 at 13:58
  • Can you please tell me how to use HashMap. –  Jul 04 '13 at 04:43

1 Answers1

-2

Just use NSMutableDictionary and put key as last name. So this will replace the old record with the new record. You will not have more than one object for a key.

AAV
  • 3,785
  • 8
  • 32
  • 59
  • What if i have multiple objects with same last name. –  Jul 05 '13 at 13:49
  • Just make the phone number as your key. You can't have more than one phone number other than a duplicate entry. – AAV Jul 05 '13 at 14:49
  • What if you have two people with the same last name and number? Such as a husband and wife that have the same last name and the same home phone number? – Abizern Jul 30 '13 at 13:14