简体   繁体   English

通过iPhone上的电话号码获取姓名

[英]Get name given by phone number on the iPhone

I'm using a TextField where the user types a phone number. 我使用的是TextField,用户可以在其中输入电话号码。 When the TextField changes, it should check if this number is already in the phonebook and display the name. 当TextField更改时,它应检查电话簿中是否已存在此号码并显示名称。

So far, my only way is to parse all names and number in a Dict and read it from there. 到目前为止,我唯一的方法是解析Dict中的所有名称和数字并从那里读取它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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