【发布时间】: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