我正在使用以下代码为我的所有联系人创建 V 卡表示。
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------------------------------------------
在此之后,我使用以下代码将联系人添加到我的地址簿:
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);
}
问题是当我将记录添加到地址簿时,它不会替换重复的联系人。它只是添加了所有的联系人,几乎所有的联系人都变成了重复的。我如何防止这种情况添加重复项。有什么方法或其他建议可以提供帮助。我认为在将联系人添加到地址簿之前,我们需要检查地址簿中是否已经存在相同的联系人。但是我们如何检查联系人是否已经在地址簿中。
提前致谢。