简体   繁体   中英

Using iOS Addressbook api to search exchange contacts

I am developing app where I would like to search for exchange contact by Name (similar to what phone contacts app does), going through AddressBook API of iOS and also searching net I am still not able to understand how can I use iOS address book api to search exchange contact. I could only find that Address book provided information that an ABSource is searchable but does not provide how to search. If any body can help it is much appreciated. Thank you Very much in advance.. I have been struggling on this for quite a long time now..

I also tried to customize ABPeoplePicker but had no much help.

The approach I took to solving this was to find the ABSource records that I wanted, then use those to get the ABPerson records in the source, then build up some data structures and filter them with NSPredicate. A bit convoluted perhaps but it seems to work.

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.

Hope this helps, let me know if you have any questions!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM