【问题标题】:Prevent duplicate contacts while adding from .vcf of all contacts using method ABPersonCreatePeopleInSourceWithVCardRepresentation使用方法 ABPersonCreatePeopleInSourceWithVCardRepresentation 从所有联系人的 .vcf 添加时防止重复联系人
【发布时间】:2013-07-03 13:52:27
【问题描述】:

我正在使用以下代码为我的所有联系人创建 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);
}

问题是当我将记录添加到地址簿时,它不会替换重复的联系人。它只是添加了所有的联系人,几乎所有的联系人都变成了重复的。我怎样才能防止这种情况添加重复项。有什么方法或其他建议可以提供帮助。我认为在将联系人添加到地址簿之前,我们需要检查地址簿中是否已经存在相同的联系人。但是我们如何检查联系人是否已经在地址簿中。

提前致谢。

【问题讨论】:

  • 如果你使用正确的数据结构,这个问题就会消失。尝试使用不允许重复的HashMap。
  • 你能告诉我如何使用HashMap。

标签: iphone ios abaddressbook vcf-vcard


【解决方案1】:

只需使用 NSMutableDictionary 并将密钥作为姓氏。所以这将用新记录替换旧记录。一个键不能有多个对象。

【讨论】:

  • 如果我有多个具有相同姓氏的对象。
  • 只需将电话号码作为您的钥匙。除了重复条目外,您不能拥有多个电话号码。
  • 如果您有两个姓氏和号码相同的人怎么办?比如姓氏相同,家庭电话号码相同的夫妻?
猜你喜欢
  • 1970-01-01
  • 2018-05-27
  • 1970-01-01
  • 2013-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-04
相关资源
最近更新 更多