我正在使用这串代码,通过它我正在检查如果一个联系人有 2 个号码,那么他们将显示 2 个同名但号码不同的联系人.. 但这段代码有时无法正确获取数据
self.contacts = [ABContactsHelper personalSorting];
NSString *emailAre;
for (int i=0; i< [self.contacts count]; i++)
{
ABContact *contObj = [self.contacts objectAtIndex:i];
if([contObj.emailDictionaries count] >=1)
emailAre = [[contObj.emailDictionaries objectAtIndex:0]objectForKey:@"value"];
// emailAre = @"";
else
emailAre = @"";
for (int j=0; j< [contObj.phoneDictionaries count]; j++)
{
if([contObj.phoneDictionaries count] != 0)
{
if([[contObj.phoneDictionaries objectAtIndex:j] count] >= 1)
{
NSLog(@"***************************************");
NSLog(@"contact Name %@",contObj.firstname);
NSLog(@"contact Number %@",[[contObj.phoneDictionaries objectAtIndex:j]objectForKey:@"value"]);
NSLog(@"***************************************");
NSString *phones = [[contObj.phoneDictionaries objectAtIndex:j]objectForKey:@"value"];
CFErrorRef anError = NULL;
ABRecordRef aRecord = ABPersonCreate();
// Set the record values
ABRecordSetValue(aRecord, kABPersonFirstNameProperty,
contObj.firstname, &anError);
ABMutableMultiValueRef multiTelephone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiTelephone, phones, kABPersonPhoneMainLabel, NULL);
ABRecordSetValue(aRecord, kABPersonPhoneProperty, multiTelephone, &anError);
/// Image
NSData *dataRef = UIImagePNGRepresentation(contObj.image);
ABPersonSetImageData(aRecord, (CFDataRef)dataRef, &anError);
/// Image
ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiEmail, emailAre, kABWorkLabel, NULL);
ABRecordSetValue(aRecord, kABPersonEmailProperty, multiEmail, &anError);
ABContact *newContract = [ABContact contactWithRecord:aRecord];
[dup addObject:newContract];
}
}
}
}