如何获取ABRecordRef的所有属性列表?

时间:2011-08-03 17:09:13

标签: iphone ios addressbook abrecordref

我想获得ABPersonRef和ABGroupRef的所有属性的列表,而不必使用kABPersonFirstNameProperty的iOS预定义键,kABPersonLastNameProperty ...我正在使用地址簿,我想迭代特定人的所有价值观。我知道有预定义的键,但Apple将来可以很好地添加新键,所以我想做类似的事情:

ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *allPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
for (int i = 0; i < [allPeople count]; ++i) {
    ABRecordRef person = [allPeople objectAtIndex:i];

    // This is the line that I can't figure out.
    NSArray *allProperties = (NSArray *)ABRecordCopyArrayOfAllProperties(person);
}

我知道我会遇到多个项目,我将不得不稍后循环,但目标是获取一个键列表,我可以迭代单值属性。我不在乎返回的类是什么,NSArray,NSDictionary ......无论如何。

我非常感谢任何建议!

2 个答案:

答案 0 :(得分:8)

您可以尝试以下操作:

使用ARC

NSDictionary* dictionaryRepresentationForABPerson(ABRecordRef person)
{
    NSMutableDictionary* dictionary = [NSMutableDictionary dictionary];

    for ( int32_t propertyIndex = kABPersonFirstNameProperty; propertyIndex <= kABPersonSocialProfileProperty; propertyIndex ++ )
    {
        NSString* propertyName = CFBridgingRelease(ABPersonCopyLocalizedPropertyName(propertyIndex));
        id value = CFBridgingRelease(ABRecordCopyValue(person, propertyIndex));

        if ( value )
            [dictionary setObject:value forKey:propertyName];
    }

    return dictionary;
}
  • 我们使用属性的本地化名称 - 在不同的语言环境中将使用不同的键。
  • 在下一版本的iOS中,属性数量可能会发生变化。

只要propertyName没有变成UNKNOWN_PROPERTY

,也许有必要了解一下属性的数量

答案 1 :(得分:2)

Aliaksandr的解决方案并不安全:例如,如果您尝试在特定ABSource中创建ABPerson记录,并使用此方法,您可能会发现联系人未正确同步到该源。

我只是从ABPerson复制了25个ABPropertyID列表,将它们放在一个简单的int []中,然后迭代它们......

        // Loop over all properties of this Person

        // taken from Apple's ABPerson reference page on 9.12.13.
        // URL: https://developer.apple.com/library/ios/documentation/AddressBook/Reference/ABPersonRef_iPhoneOS/Reference/reference.html#//apple_ref/c/func/ABPersonGetTypeOfProperty

        // count = 25. All are type ABPropertyID

        int propertyArray[25] = {
            kABPersonFirstNameProperty,
            kABPersonLastNameProperty,
            kABPersonMiddleNameProperty,
            kABPersonPrefixProperty,
            kABPersonSuffixProperty,
            kABPersonNicknameProperty,
            kABPersonFirstNamePhoneticProperty,
            kABPersonLastNamePhoneticProperty,
            kABPersonMiddleNamePhoneticProperty,
            kABPersonOrganizationProperty,
            kABPersonJobTitleProperty,
            kABPersonDepartmentProperty,
            kABPersonEmailProperty,
            kABPersonBirthdayProperty,
            kABPersonNoteProperty,
            kABPersonCreationDateProperty,
            kABPersonModificationDateProperty,

            kABPersonAddressProperty,
            kABPersonDateProperty,
            kABPersonKindProperty,
            kABPersonPhoneProperty,
            kABPersonInstantMessageProperty,
            kABPersonSocialProfileProperty,
            kABPersonURLProperty,
            kABPersonRelatedNamesProperty
        };
        int propertyArraySize = 25;



        for ( int propertyIndex = 0; propertyIndex < propertyArraySize; propertyIndex++ ) {
...code here
}