Search code examples
iphoneobjective-ccocoa-touchiosabaddressbooksource

How do I get count of people from ExchangeGal sources using ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering()


I am trying to get people in three sources (1 MobileMe source and 2* ExchangeGAL sources) of my iOS 4 addressbook.
The NSLog statement always return 0 people for ExchangeGAL, but returns a count of people for MobileMe.
After getting all the sources from the iOS address book by using ABAddressBookCopyArrayOfAllSources(addressBook), I am trying to iterate source by source to get more info for each source.

Am I using the ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering correctly?
Would expect a count of all people in the different addressbook sources.

-(void)countPeopleInSources
{
    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef allSources = ABAddressBookCopyArrayOfAllSources(addressBook);
    for (CFIndex i = 0; i < CFArrayGetCount(allSources); i++) 
    {
        ABRecordRef source = (ABRecordRef)CFArrayGetValueAtIndex(allSources, i);
        NSNumber *sourceTypeRef = (NSNumber *)((CFNumberRef)ABRecordCopyValue(source, kABSourceTypeProperty));
        ABPersonSortOrdering sortOrdering = ABPersonGetSortOrdering();
        int sourceType = [sourceTypeRef intValue];
        switch (sourceType) {
        case kABSourceTypeCardDAV:
            NSLog(@"SourceTypeCardDAV");
            break;
        case kABSourceTypeCardDAVSearch:
            NSLog(@"SourceTypeCardDAVSearch");
            break;
        case kABSourceTypeExchange:
            NSLog(@"SourceTypeExchange");
            break;
        case kABSourceTypeExchangeGAL:
            NSLog(@"SourceTypeExchangeGAL");
            break;
        case kABSourceTypeLDAP:
            NSLog(@"SourceTypeLDAP");
            break;
        case kABSourceTypeLocal:
            NSLog(@"SourceTypeLocal");
            break;
        case kABSourceTypeMobileMe:
            NSLog(@"SourceTypeMobileMe"); 
            break;
        default:
            break;
    }
        CFArrayRef allPeopleInSource = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, sortOrdering );
        NSLog(@"Count allPeopleInSource: %i", CFArrayGetCount(allPeopleInSource));

        [sourceTypeRef release];
    }
}

EDIT FEB24: It looks like the code works, but I have a problem getting people count from ExhangeGALs.
Has anyoune successfully retrieved people from ExchangeGAL?
MobileMe source reports back a people count.
Here is the log from Console:

2011-02-24 07:04:32.578 Contacts[10099:307] SourceTypeExchangeGAL
2011-02-24 07:04:32.593 Contacts[10099:307] Count allPeopleInSource: 0
2011-02-24 07:04:32.597 Contacts[10099:307] SourceTypeMobileMe
2011-02-24 07:04:32.641 Contacts[10099:307] Count allPeopleInSource: 151
2011-02-24 07:04:32.646 Contacts[10099:307] SourceTypeExchangeGAL
2011-02-24 07:04:32.652 Contacts[10099:307] Count allPeopleInSource: 0

Solution

  • Try Using:

    ABRecordRef sourceWithType (ABSourceType mySourceType)
    {
    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef sources = ABAddressBookCopyArrayOfAllSources(addressBook);
    CFIndex sourceCount = CFArrayGetCount(sources);
    ABRecordRef resultSource = NULL;
    for (CFIndex i = 0 ; i < sourceCount; i++) {
        ABRecordRef currentSource = CFArrayGetValueAtIndex(sources, i);
        ABSourceType sourceType = [(NSNumber *)ABRecordCopyValue(currentSource, kABSourceTypeProperty) intValue];
        if (mySourceType == sourceType) {
            resultSource = currentSource;
            break;
        }
    }
    
    return resultSource;
    }
    
    ABRecordRef localSource()
    {
        return sourceWithType(kABSourceTypeLocal);
    }
    
    ABRecordRef exchangeSource()
    {
         return sourceWithType(kABSourceTypeExchange);
    }
    
    ABRecordRef mobileMeSource()
    {
         return sourceWithType(kABSourceTypeMobileMe);
    }
    

    You might find this helpful :abaddressbook-absource-and-absourcetype