【问题标题】:How to insert Multiple contacts into ABAddressbook?如何将多个联系人插入 ABAddressbook?
【发布时间】:2013-01-16 21:00:23
【问题描述】:

我需要向 iPhone 联系人插入 25000 个联系人,就我编写的代码而言,它在模拟器上运行良好,并且可以在几分钟内完成。但是当我在 iPhone 4s 上导入它时,需要 3 个多小时,而且只有部分联系人被保存,然后应用程序被关闭。

帮我用一种简单的方法将多条记录插入通讯录...

这是我的一段代码...

for (int i = 0; i < [contactNameArray count]; i++) {
    //Create new person and save to this group
    ABRecordRef record = ABPersonCreate();
    BOOL isSuccess ;
    NSString *firstname = [NSString stringWithFormat:@"%@",[contactNameArray objectAtIndex:i]];
    isSuccess  = ABRecordSetValue(record, kABPersonFirstNameProperty,(__bridge CFStringRef)firstname, &error); 
    isSuccess  = ABRecordSetValue(record, kABPersonLastNameProperty,CFSTR("Custom Contacts"), &error); 

    ABMutableMultiValueRef copyOfPhones = ABMultiValueCreateMutable(kABPersonPhoneProperty);
    NSString *phonenumber = [NSString stringWithFormat:@"%@",[contactPhoneArray objectAtIndex:i]];
    CFTypeRef phone= (__bridge CFStringRef)phonenumber;
    ABMultiValueAddValueAndLabel(copyOfPhones, phone,kABPersonPhoneMobileLabel,NULL);
    isSuccess = ABRecordSetValue(record, kABPersonPhoneProperty, copyOfPhones, &error);
    CFRelease(copyOfPhones);

    ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    NSString *emailid = [NSString stringWithFormat:@"%@",[contactEmailArray objectAtIndex:i]];
    CFTypeRef email= (__bridge CFStringRef)emailid;
    ABMultiValueAddValueAndLabel(multiEmail, email, kABWorkLabel, NULL);
    ABRecordSetValue(record, kABPersonEmailProperty, multiEmail, &error);
    CFRelease(multiEmail);

    isSuccess = ABAddressBookAddRecord(ab, record, &error);
    isSuccess = ABAddressBookSave(ab, &error);

    ABGroupAddMember(group, record, &error);

    NSLog(@"is success %d", isSuccess);

    ABAddressBookSave(ab, &error);
}

提前谢谢....

【问题讨论】:

  • 不要忘记模拟器拥有 Mac 的全部功能和内存,而 iPhone 没有。
  • 是的,我知道,但我需要一种在任何 iOS 设备上快速插入的方法......

标签: iphone ios contacts abaddressbook


【解决方案1】:

首先使用快速枚举。

然后批量保存联系人,并在批量结束时将计数器编号保存到用户默认值或任何位置。这将阻止您再次重新开始应对过程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-16
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    相关资源
    最近更新 更多