これを解決するために私が取ったアプローチは、必要な 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.
ご不明な点がございましたら、お気軽にお問い合わせください。