简体   繁体   中英

Get name given by phone number on the iPhone

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?

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.

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM