【问题标题】:How to search a contact's phone number? (Abaddressbook)如何搜索联系人的电话号码? (地址簿)
【发布时间】:2013-04-01 18:32:55
【问题描述】:

在我的 iOS 应用中,我想查找与姓名匹配的联系人的电话号码。

CFErrorRef *error = NULL;

// Create a address book instance.
ABAddressBookRef addressbook = ABAddressBookCreateWithOptions(NULL, error);

// Get all people's info of the local contacts.
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressbook);
CFIndex numPeople = ABAddressBookGetPersonCount(addressbook);
for (int i=0; i < numPeople; i++) {

    // Get the person of the ith contact.
    ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
    ## how do i compare the name with each person object?
}

【问题讨论】:

    标签: ios objective-c abaddressbook


    【解决方案1】:

    你可以通过ABRecordCopyCompositeName获取人名,并测试搜索字符串是否包含在CFStringFind的人名中。

    例如,查找姓名中带有“Appleseed”的联系人:

    CFStringRef contactToFind = CFSTR("Appleseed");
    
    CFErrorRef *error = NULL;
    
    // Create a address book instance.
    ABAddressBookRef addressbook = ABAddressBookCreateWithOptions(NULL, error);
    
    // Get all people's info of the local contacts.
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressbook);
    CFIndex numPeople = ABAddressBookGetPersonCount(addressbook);
    for (int i=0; i < numPeople; i++) {
    
        // Get the person of the ith contact.
        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
    
        // Get the composite name of the person
        CFStringRef name = ABRecordCopyCompositeName(person);
    
        // Test if the name contains the contactToFind string
        CFRange range = CFStringFind(name, contactToFind, kCFCompareCaseInsensitive);
    
        if (range.location != kCFNotFound)
        {
            // Bingo! You found the contact
            CFStringRef message = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("Found: %@"), name);
            CFShow(message);
            CFRelease(message);
        }
    
        CFRelease(name);
    }
    
    CFRelease(allPeople);
    CFRelease(addressbook);
    

    希望这会有所帮助。虽然您的问题是 5 个月前提出的......

    【讨论】:

      猜你喜欢
      • 2011-04-12
      • 1970-01-01
      • 2012-04-26
      • 2014-11-01
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多