ABAddressBook ABSource and ABSourceType

你说的曾经没有我的故事 提交于 2019-11-27 13:22:33

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.

xyzzycoder

I posted some code here: Obtaining Specific ABSource from ABAddressBook in iOS 4+

for identifying specific sources. It may be useful in helping you to see how to work with ABAddressBook.

Update for Mixja's response

  1. ABAddressBookCreate() has been deprecated. Declare a reference in interface:
    • Also, remove: addressBook = ABAddressBookCreate();
  2. Update the declaration of sourceTypeRef & sourceTypeName:
  3. Change the release method calls to:

    // 1.
    @property(nonatomic, assign) ABAddressBookRef *addressBook;
    // 2.
    NSNumber *sourceTypeRef = (__bridge NSNumber *)((CFNumberRef)ABRecordCopyValue(source, kABSourceTypeProperty));
    NSString *sourceTypeName = (__bridge NSString *)((CFStringRef)ABRecordCopyValue(source, kABSourceNameProperty));
    
    // 3.
    CFRelease((__bridge CFTypeRef)(sourceTypeRef));
    CFRelease((__bridge CFTypeRef)(sourceTypeName));
    

Final code should look something like this:

Make sure to include step 1!

    ABRecordRef searchableExchangeSource;

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

    // Get source properties
    NSNumber *sourceTypeRef = (__bridge NSNumber *)((CFNumberRef)ABRecordCopyValue(source, kABSourceTypeProperty));
    NSString *sourceTypeName = (__bridge NSString *)((CFStringRef)ABRecordCopyValue(source, kABSourceNameProperty));
    int sourceType = [sourceTypeRef intValue];
    NSLog(@"Found Source Type: %@ with ABSourceType %i", sourceTypeName,sourceType);
    if (sourceType == kABSourceTypeExchangeGAL) {
        searchableExchangeSource = source;
    }
    CFRelease((__bridge CFTypeRef)(sourceTypeRef));
    CFRelease((__bridge CFTypeRef)(sourceTypeName));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!