我使用以下代码为我的所有联系人创建一个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);
}
问题是当我将记录添加到地址簿时,它不会替换重复的联系人。它只是添加了所有联系人,几乎所有联系人都变成了重复。如何防止此添加重复项。是否有任何方法或其他建议可以提供帮助。我认为在将联系人添加到地址簿之前,我们需要检查地址簿中是否存在相同的联系人。但是我们如何检查联系人是否已经存在于地址簿中。
提前致谢。
答案 0 :(得分:-2)
只需使用NSMutableDictionary并将密钥作为姓氏。所以这将用新记录取代旧记录。对于密钥,您不会有多个对象。