0

I'm using a TextField where the user types a phone number. When the TextField changes, it should check if this number is already in the phonebook and display the name.

So far, my only way is to parse all names and number in a Dict and read it from there.

Is there a simpler, more efficient and sophisticated way to do that?

endo.anaconda
  • 2,449
  • 4
  • 29
  • 55

2 Answers2

0

It wouldn't be hard to rip through the user's address book and create a phone number to person mapping. The Address Book Programming Guide lays out all the information on how the framework works.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
0

To close and complete this issue here is the main part of my solution:

ABAddressBookRef m_addressbook = ABAddressBookCreate();
if (!m_addressbook) {
    NSLog(@"opening address book");
}

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);

[.....]

adressList = [[NSMutableDictionary alloc] init];

for (int i=0;i < nPeople;i++) { 
    ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);

    vorname = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
    nachname = ABRecordCopyValue(ref, kABPersonLastNameProperty);
    nameTag = [NSString stringWithFormat:@"%@ %@.", vorname, nachname];

    ABMultiValueRef phones =(NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty);

    // Loop thru all numbers of a person

    for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
        tmpNumber = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
        tmpNumber = [self cleanupPhoneNumber:tmpNumber];
        [adressList setObject: nameTag forKey:tmpNumber];
        NSLog(@"Name: %@ | Phone: %@", nameTag, tmpNumber);
    }
}
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
endo.anaconda
  • 2,449
  • 4
  • 29
  • 55
  • You need to release `vorname`, `nachname`, `phones`, and `tmpNumber`, because they're all produced by a function which has `Copy` in the name. Also, you're explicitly leaking `tmpNumber` because you get a reference to an object with a +1 retain count, them immediately lose the reference as you run it through `cleanupPhoneNumber:` – Dave DeLong May 02 '10 at 14:17