如何避免地址簿中的重复联系人?

时间:2014-03-20 07:00:55

标签: ios iphone objective-c addressbook

嗨,我正在从地址簿中检索联系人,但我也会收到重复内容,例如:

  
      
  1. sam - 1234567890
  2.   
  3. sam - 1234567890
  4.   

出现在地址簿中,两者都有,是否有任何属性可以避免这种情况。虽然它的作用是相同名称的数字不同。 我的代码:

-(void)getContactsFromAddressBook:(ABRecordRef)person
{
    NSMutableArray *arrayContactsFromAddressbook;
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    NSMutableDictionary *dictContacts=[[NSMutableDictionary alloc] init];

    ABRecordID recordId = ABRecordGetRecordID(person);
    [dictContacts setObject:[NSNumber numberWithInt:recordId] forKey:@"recordId"];

    NSString *firstName = (__bridge  NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
    NSString *lastName = (__bridge  NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
    if(firstName!=nil)
        [dictContacts setObject:firstName forKey:@"firstName"];
    if(lastName!=nil)
        [dictContacts setObject:lastName forKey:@"lastName"];
    else
        [dictContacts setObject:@"" forKey:@"lastName"];
    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString *strPhoneNumber;
    NSMutableDictionary *dictPhoneNumbers = [[NSMutableDictionary alloc] init];
    for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++)
    {
        strPhoneNumber = (__bridge  NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
        CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(phoneNumbers, i);
        NSString *phoneLabel = (__bridge NSString*) ABAddressBookCopyLocalizedLabel(locLabel);

        NSString * strippedNumber = [strPhoneNumber stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [strPhoneNumber length])];
        if ([strippedNumber length]>=10)
        {
            NSString *trimmedString = [strippedNumber substringFromIndex:[strippedNumber length]-10];
            [dictPhoneNumbers setObject:trimmedString forKey:phoneLabel];
        }
//       CFRelease(locLabel);
    }
    [dictContacts setObject:dictPhoneNumbers forKey:@"phoneNumbers"];

    if ([[dictContacts objectForKey:@"phoneNumbers"] count])
    {
        [arrayContactsFromAddressbook addObject:dictContacts];
    }
}

arrayContactsFromAddressbook包含地址簿中的所有联系人。如何根据电话号码删除重复项?

0 个答案:

没有答案