iphone - 如何将多个联系人插入ABAddressbook?

标签 iphone ios contacts abaddressbook

我需要将 25000 个联系人插入 iPhone 联系人,据我编码,它在模拟器上运行良好,并且可以在几分钟内实现。但当我将其导入 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);
}

提前致谢......

最佳答案

首先使用快速枚举。

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

关于iphone - 如何将多个联系人插入ABAddressbook?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14359023/

相关文章:

iphone - 尝试使用 NSDictionary 值设置 numberOfRowsInSection

ios - 取消 Objective C 中的 LAContext 评估

ios - 重新定义 'xyz' Cocoapods

ios - 从其 subview Controller 之一中删除 UIPageViewController 的正确过程是什么?

iOS 地址簿记录存在于地址簿数据库之外

java - 如何按标题获取组ID?

iphone - UINavigationController 添加 UIView

ios - 添加静态Realm.framework但编译时出现错误: framework not found Realm clang: error: linker command failed with exit code 1

ios - Apple 联系人使用政策

iPhone - 队列中操作之间的延迟