나는 이것을 해결하기 위해 취한 접근법은 내가 원한 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.
질문이 있으시면 알려주십시오.
이봐 요, 혹시 이것에 대한 해결책을 찾았습니까? ABAddressBookCopyArrayOfAllPeopleInSource는 항상 원본 유형이 kABSourceTypeExchangeGAL 인 빈 배열을 반환합니다. 그러나이 유형에는 "kABSourceTypeSearchableMask"에 대한 비트가 포함되어 있으므로 검색 가능하다고 생각할 수 있습니다. 그것을 검색 할 다른 방법이 있습니까? – Sam