我正在开发应用程序,我希望通过名称搜索交换联系人(类似于手机通讯录的应用程序),通过iOS的AddressBook API以及搜索网络我仍然无法理解如何使用iOS地址簿api搜索交换联系人。 我只能找到地址簿提供的信息,ABSource是可搜索的,但不提供如何搜索。如果有任何身体可以帮助它非常感谢。非常感谢你提前..我已经在这很长一段时间里一直在努力...
我也试图自定义ABPeoplePicker,但没有太多帮助。
答案 0 :(得分: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.
希望这有帮助,如果您有任何问题,请与我联系!