有没有人举过一个例子,说明如何从iOS 4+中的ABAddressBook获取特定的ABSource?

最佳答案

iOS 4+提供了新的API,允许人们从ABAddressBook中选择特定的ABSource。这对于某些操作可能很有用,例如某些来源(例如Exchange)不支持创建ABGroup。

“并非所有源类型都支持组,更明显的是,Exchange对组一无所知。” -http://flavors.me/volonbolon#1a5/tumblr

附带的函数利用新API来获取特定类型的源,这些源可以在对ABGroupCreateInSource()的调用中使用。

#define CFRELEASE_AND_NIL(x) CFRelease(x); x=nil;
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);
        CFTypeRef sourceType = ABRecordCopyValue(currentSource, kABSourceTypeProperty);

        BOOL isMatch = mySourceType == [(NSNumber *)sourceType intValue];
        CFRELEASE_AND_NIL(sourceType);

        if (isMatch) {
            resultSource = currentSource;
            break;
        }
    }

    CFRELEASE_AND_NIL(addressBook);
    CFRELEASE_AND_NIL(sources);

    return resultSource;
}

ABRecordRef localSource()
{
    return sourceWithType(kABSourceTypeLocal);
}

ABRecordRef exchangeSource()
{
    return sourceWithType(kABSourceTypeExchange);
}

ABRecordRef mobileMeSource()
{
    return sourceWithType(kABSourceTypeMobileMe);
}

09-10 02:41