地址簿ABPerson Twitter社交档案

时间:2012-01-31 02:48:49

标签: ios addressbook

你有没有试过从地址簿中获取数据字段并进入ABPerson参考的奇妙世界,却发现它读起来像太空火箭的蓝图?

我已经到目前为止但我仍然需要帮助才能获得Twitter用户名密钥&值:

//I tried this but I can't seem to get the if statement to work
ABMutableMultiValueRef socialMulti = ABRecordCopyValue(person, kABPersonSocialProfileProperty);
NSMutableDictionary *mySocialDict = [NSMutableDictionary dictionaryWithCapacity:ABMultiValueGetCount(socialMulti)];
NSLog(@"entering social dict of count %ld", ABMultiValueGetCount(socialMulti));
for (CFIndex i = 0; i < ABMultiValueGetCount(socialMulti); i++) { 
    CFStringRef socialLabel = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(socialMulti, i));
    CFStringRef social = ABMultiValueCopyValueAtIndex(socialMulti, i); 
    if ([(__bridge NSString*)socialLabel isEqualToString:@"twitter"]) {
        NSLog(@"we got a twitter");
    }
    [mySocialDict setObject:(__bridge NSString*)social forKey:(__bridge NSString*)socialLabel];
    NSLog(@"social is %@",social);

    CFRelease(social);
    CFRelease(socialLabel);

}

我实际上只对twitter用户名感兴趣。我知道我可以从我创建的字典中获取它,但我想直接得到它。无论如何,我打算取消NSDictionary步骤。

1 个答案:

答案 0 :(得分:2)

以下是我的代码的摘录。根据需要替换您的var名称。

ABMultiValueRef socialMulti = ABRecordCopyValue(recordRef, kABPersonSocialProfileProperty);
NSMutableArray* twitterHandlesArray = [[NSMutableArray alloc] initWithCapacity:ABMultiValueGetCount(socialMulti)];
for (CFIndex i = 0; i < ABMultiValueGetCount(socialMulti); i++) {
    NSDictionary* social = (__bridge NSDictionary*)ABMultiValueCopyValueAtIndex(socialMulti, i);
    if ([social[@"service"] isEqualToString:(__bridge NSString*)kABPersonSocialProfileServiceTwitter]) {
        NSString* username = (NSString*)social[@"username"];
        NSLog(@"we got a twitter. username is %@", username);

        [twitterHandlesArray addObject:[[username conditionedAsTwitterHandle] SHA2Digest]];
    }
}