2

Im trying to find a phone number in the address book from my app and I was surprised not to find it. The thing is that I've printed all the numbers of my address book in the console accessed by my app and strangely some of the contacts are missing, I was comparing the output with my address book, it's only few, but still.

This is how Im accesing the AddressBook:

 ABAddressBookRef addressBook = ABAddressBookCreate();
 NSArray *people = (NSArray *)   ABAddressBookCopyArrayOfAllPeople(addressBook);

 BOOL found = NO;
 NSString *name;
 int i = 0;
 while (!found) {//Here I print all the contact info, name and phone number
       ABRecordRef person = (ABRecordRef)[people objectAtIndex:i];
       ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
       NSLog(@"el telf: %@ y nombre %@",tempPhone2, [NSString stringWithFormat:@"%@ %@",ABRecordCopyValue(person, kABPersonFirstNameProperty) ? (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) : @"",ABRecordCopyValue(person, kABPersonLastNameProperty) ? (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty) : @""]);                           
      if([[key objectForKey:@"phone"] isEqualToString:tempPhone2]){
           found = YES;          
      }
 }

Any idea why Im not accesing all the contacts in my Address Book?

[EDIT] Weirdest thing is that when I use ABPeoplePickerNavigationController those missing contacts appear.

subharb
  • 3,374
  • 8
  • 41
  • 72

1 Answers1

2

Please try the below.

ABAddressBookRef lAddressBook = ABAddressBookCreate();
CFArrayRef lRawAddressBookEntries =
ABAddressBookCopyArrayOfAllPeople(lAddressBook);
CFIndex lTotalContactsCount = ABAddressBookGetPersonCount(lAddressBook);
for (CFIndex i = 0; i < lTotalContactsCount; i++) {
    ABRecordRef lRef = CFArrayGetValueAtIndex(lRawAddressBookEntries, i);

    ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(lRef, kABPersonPhoneProperty);
    NSArray* phoneNumbers1 = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);
    CFRelease(phoneNumberProperty);

    // Do whatever you want with the phone numbers
    if(phoneNumbers1 && [phoneNumbers1 isKindOfClass:[NSArray class]]) {
        for(NSString *stringPhoneNUmber in phoneNumbers1) {
            if([stringPhoneNUmber isEqualToString:tempPhone2]){
                found = YES;
            }
        }
    }
}
Paramasivan Samuttiram
  • 3,728
  • 1
  • 24
  • 28
  • Can you explain what went wrong in OP's original code? :) – Flying_Banana Apr 26 '15 at 01:09
  • I do not know what he missed there, but it is just a working code, i am using in my projects. – Paramasivan Samuttiram Apr 27 '15 at 07:32
  • If you use `ABAddressBookCopyArrayOfAllPeople` and `ABMultiValueCopyArrayOfAllValues`, make sure to `CFRelease` them (and the `lAddressBook`, too). Or you can use `CFBridgingRelease` to enjoy toll-free bridging from the `CFArrayRef` to a `NSArray`, and then ARC will take care of the memory management for you. But as it stands, this code will leak all of the contacts and also all of the phone numbers. If you use the static analyzer, it should help you identify all of the leaks here. – Rob Oct 13 '15 at 18:54