Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

iphone - ABAddressBook ABSource and ABSourceType

I am attempting to create an app that can be used to search an Exchange GAL, however, I am finding the new 4.0 documentation regarding this subject confusing. Does anyone know how I might go about searching the GAL for names containing a specific string (e.g. "Smi")? My source code at the moment is all but useless as I am simply trying to wrap my head around how to specify that I am wanting only to search the GAL and not the local contacts on the device. Also, how is kABSourceTypeSearchableMask used? I am missing something fundamental here. From the documentation...

Source Types

These constants identify the type of a source.

enum {
    kABSourceTypeLocal       = 0x0,
    kABSourceTypeExchange    = 0x1,
    kABSourceTypeExchangeGAL = kABSourceTypeExchange | kABSourceTypeSearchableMask,
    kABSourceTypeMobileMe    = 0x2,
    kABSourceTypeLDAP        = 0x3 | kABSourceTypeSearchableMask,
    kABSourceTypeCardDAV     = 0x4,
    kABSourceTypeCardDAVSearch = kABSourceTypeCardDAV | kABSourceTypeSearchableMask,
};
typedef int ABSourceType;

When I query for the default source type, I do get "1" which would appear to indicate that the default type is "kABSourceTypeExchange" which would be correct as this is what I have in my Settings. I do not know how to proceed beyond this point...

As the whole source concept is a new to the ABAddressBook framework in 4.0 I don't imagine that folks have much experience with this, but hoping someone might help me understand how to work with the above...thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To get access to the Exchange GAL, you'll need to use the function ABAddressBookCopyArrayOfAllSources to get an array of all sources, and then iterate over the array to try and get the correct source for the Exchange GAL. Use the ABRecordCopyValue() function to get the kABSourceTypeProperty property of the source.

e.g.

ABRecordRef searchableExchangeSource;

addressBook = ABAddressBookCreate();
CFArrayRef allSources = ABAddressBookCopyArrayOfAllSources(addressBook);
for (CFIndex i = 0; i < CFArrayGetCount(allSources); i++) {
    ABRecordRef source = (ABRecordRef)CFArrayGetValueAtIndex(allSources, i);

    // Get source properties
    NSNumber *sourceTypeRef = (NSNumber *)((CFNumberRef)ABRecordCopyValue(source, kABSourceTypeProperty));
    NSString *sourceTypeName = (NSString *)((CFStringRef)ABRecordCopyValue(source, kABSourceNameProperty));
    int sourceType = [sourceTypeRef intValue];
    NSLog(@"Found Source Type: %@ with ABSourceType %i", sourceTypeName,sourceType);
    if (sourceType == kABSourceTypeExchangeGAL) {
        searchableExchangeSource = source;
    }
    [sourceTypeRef release];
    [sourceTypeName release];
}

Note if you have multiple "Exchange" accounts set up you will get multiple sources with the same ABSourceType. Unfortunately from my limited testing, the kABSourceTypeNameProperty for Exchange GALs is NULL so you can't use this property to differentiate between multiple Exchange GAL sources.

Once you have the appropriate source, it is of the type ABRecordRef so you can interact with it just like any other record.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...