3

我正在使用以下代码从 iOS 6 中的通讯录中检索社交资料:

// get the address book
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(options, error);

// get all people
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);

// get the number of people
CFIndex noOfPeople = ABAddressBookGetPersonCount(addressBook);

// for all people
for (CFIndex personIndex = 0; personIndex<noOfPeople; personIndex++)
{
    // get social profiles
    NSMutableArray *socialProfilesArray = [[NSMutableArray alloc] init];
    ABMultiValueRef socialProfiles = ABRecordCopyValue(person, kABPersonSocialProfileProperty);

    for(CFIndex j = 0; j < ABMultiValueGetCount(socialProfiles); j++)
    {
        NSDictionary* socialProfile = (NSDictionary*)ABMultiValueCopyValueAtIndex(socialProfiles, j);

        if ([socialProfile[@"service"] isEqualToString:(NSString*)kABPersonSocialProfileServiceFacebook])
        {
            NSString *strFacebook = (NSString*)socialProfile[@"username"];
        }
        else if ([socialProfile[@"service"] isEqualToString:( NSString*)kABPersonSocialProfileServiceTwitter])
        {
            NSString *twitterName = (NSString*)socialProfile[@"username"];
        }

        [socialProfilesArray addObject:socialProfile];
        [socialProfile release];
    }
    CFRelease(socialProfiles);
}

看来,虽然我成功获得了 Twitter 个人资料,但没有返回 Facebook 个人资料。尽管如联系人应用程序中所示,显然有一个 Facebook 个人资料与我的一个联系人相关联。

任何想法为什么?

4

1 回答 1

1

嗨,我想我解决了这个问题,我自己回答了问题:

ABMultiValueGetCount - kABPersonSocialProfileProperty 总是返回 0 吗?如何从通讯录中获取 Facebook 详细信息?

这可能是因为用户必须通过 iOS 上的按钮手动更新 facebook,或者记录不显示。我被困在眼前的事实,我可以在地址簿中看到 Facebook 联系人,但社交属性给了我 nada !一旦我更新并扫描了链接的联系人,我就找到了我需要的一切。还包括我的代码:

/*  person is of type ABRecordRef loaded from an array thus:
    addressBookArray = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
    .....
    person = (__bridge ABRecordRef)[addressBookArray objectAtIndex:indexPath.section];
*/


ABAddressBookRef addressBook;
addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

if (addressBook != Nil)
{
    int howManySocialApps;
    ABRecordRef who = ABAddressBookGetPersonWithRecordID (addressBook, ABRecordGetRecordID(person));

    NSArray *linkedPeople = (__bridge_transfer NSArray *)ABPersonCopyArrayOfAllLinkedPeople (who);

    for (int x = 0; x < [linkedPeople count]; x++)
    {
        ABMultiValueRef socialApps = ABRecordCopyValue((__bridge ABRecordRef)[linkedPeople objectAtIndex:x], kABPersonSocialProfileProperty);

        CFIndex thisSocialAppCount = ABMultiValueGetCount(socialApps);

        for (int i = 0; i < thisSocialAppCount; i++)
        {
            NSDictionary *socialItem = (__bridge_transfer NSDictionary*)ABMultiValueCopyValueAtIndex(socialApps, i);
            NSLog(@"Social Item of type %@", [socialItem objectForKey:(NSString *)kABPersonSocialProfileServiceKey]);
            /* put tests for facebook etc here */
        }

        if (socialApps != Nil)
            CFRelease(socialApps);

        howManySocialApps += thisSocialAppCount;
    }

    NSLog (@"Number of SocialApps Found is %i", howManySocialApps);

    CFRelease(addressBook);
}

我发现有用的另一件事是确保我的数组被操纵没有很多重复的联系人,即 FB、Twitter 等的入口用户。Apple 有一些很好的代码来做到这一点:

    ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
    addressBookArray = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByLastName);

然后使用链接的联系人,我们可以找到任何东西。

干杯。

于 2013-04-13T02:59:39.217 回答