0

hi i am retrieving contacts from address book but i am getting duplicates too for eg:

  1. sam - 1234567890
  2. sam - 1234567890

are present in address book it gets both, is there any property to avoid this case. While it works if the number is different for same name. My code:

-(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];
    }
}

the arrayContactsFromAddressbook contains all the contacts from the address book. How do i remove duplicates based on phone number?

Mehmet Emre Portakal
  • 1,774
  • 21
  • 37
user2798258
  • 171
  • 1
  • 4
  • 13

0 Answers0