3

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.

Xcoder
  • 51
  • 5
  • Hey, did you ever find the solution to this? ABAddressBookCopyArrayOfAllPeopleInSource always returns an empty array for source type of kABSourceTypeExchangeGAL. But since this type includes the bit for "kABSourceTypeSearchableMask" you'd think it would be searchable. Is there another way to search it? – Sam Jul 09 '13 at 17:09

1 Answers1

0

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!

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81