3

我正在开发应用程序,我想按名称搜索交换联系人(类似于电话联系人应用程序),通过 iOS 的 AddressBook API 并搜索网络我仍然无法理解如何使用 iOS 通讯录 API搜索交换联系人。我只能发现地址簿提供了 ABSource 可搜索但不提供如何搜索的信息。如果有任何机构可以提供帮助,我们将不胜感激。非常感谢您..我已经为此苦苦挣扎了很长时间..

我也尝试自定义 ABPeoplePicker,但没有太多帮助。

4

1 回答 1

0

我解决这个问题的方法是找到我想要的 ABSource 记录,然后使用这些记录来获取源中的 ABPerson 记录,然后构建一些数据结构并使用 NSPredicate 过滤它们。也许有点令人费解,但它似乎有效。

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef sources = ABAddressBookCopyArrayOfAllSources(addressBook);
CFIndex sourcesCount = CFArrayGetCount(sources);
ABRecordRef sourceToSearch = NULL;
for (CFIndex index = 0; index < sourcesCount; index++)
{
    ABRecordRef record = (ABRecordRef)CFArrayGetValueAtIndex(sources, index);
    NSNumber *sourceTypeNumber = (__bridge NSNumber *)(CFNumberRef)ABRecordCopyValue(record, kABSourceTypeProperty);
    ABSourceType sourceType = [sourceTypeNumber intValue];
    if (sourceType == 4) //this was the only source type with people on my phone, I guess you'll use kABSourceTypeExchange instead
    {
        sourceToSearch = record;
        break;
    }
}

CFArrayRef peopleInRecord = (CFArrayRef)ABAddressBookCopyArrayOfAllPeopleInSource(addressBook, sourceToSearch);
CFIndex peopleCount = CFArrayGetCount(peopleInRecord);
NSMutableArray *peopleDictionaries = [NSMutableArray array];
for (CFIndex index = 0; index < peopleCount; index++)
{
    ABRecordRef personRecord = CFArrayGetValueAtIndex(peopleInRecord, index);
    ABRecordID recordID = ABRecordGetRecordID(personRecord);
    NSString *personName = (__bridge NSString *)(CFStringRef)ABRecordCopyValue(personRecord, kABPersonFirstNameProperty);
    if (personName)
    {
        NSDictionary *personDictionary = @{ @"recordID" : [NSNumber numberWithInt:recordID], @"name" : personName };
        [peopleDictionaries addObject:personDictionary];
    }
}

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",@"name",@"Kyle"];
NSArray *kyles = [peopleDictionaries filteredArrayUsingPredicate:predicate];
NSLog(@"filtered dictionarys = %@",kyles);
/*
 2012-08-27 17:26:24.679 FunWithSO[21097:707] filtered dictionaries = (
 {
 name = Kyle;
 recordID = 213;
 }
 )*/
//From here, get the recordID instance and go get your ABPerson Records directly from the address book for further manipulation.

希望这会有所帮助,如果您有任何问题,请告诉我!

于 2012-08-27T23:31:34.853 回答