【问题标题】:Using iOS Addressbook api to search exchange contacts使用 iOS Addressbook api 搜索交换联系人
【发布时间】:2012-05-21 14:52:06
【问题描述】:

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

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

【问题讨论】:

  • 嘿,你找到解决办法了吗? ABAddressBookCopyArrayOfAllPeopleInSource 始终为 kABSourceTypeExchangeGAL 的源类型返回一个空数组。但由于这种类型包含“kABSourceTypeSearchableMask”位,您会认为它是可搜索的。还有其他方法可以搜索吗?

标签: iphone ios abpeoplepickerview


【解决方案1】:

我解决这个问题的方法是找到我想要的 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.

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    相关资源
    最近更新 更多