简体   繁体   English

从通讯录中获取Twitter用户

[英]Getting twitter user from Address Book

I'm trying to get the Twitter username from a contact in the address book but I'm not able to do it 我正在尝试从通讯录中的联系人获取Twitter用户名,但我做不到

I'm using this code that I found "googling": 我正在使用以下代码,这些代码我发现“正在搜索”:

- (NSString*)getTwitterUsernameFromRecord:(ABRecordRef)record {
    NSString * twitterUsername = nil;

    ABMultiValueRef socials = ABRecordCopyValue(record, kABPersonSocialProfileProperty);

    if (!socials) {
        return nil;
    }

    CFIndex socialsCount = ABMultiValueGetCount(socials);

    for (int k=0 ; k<socialsCount ; k++) {
        CFDictionaryRef socialValue = ABMultiValueCopyValueAtIndex(socials, k);
        if(CFStringCompare( CFDictionaryGetValue(socialValue, kABPersonSocialProfileServiceKey), kABPersonSocialProfileServiceTwitter, 0)==kCFCompareEqualTo) {
            twitterUsername = (NSString*) CFDictionaryGetValue(socialValue, kABPersonSocialProfileUsernameKey);
        }
        CFRelease(socialValue);

        if(twitterUsername)
            break;
    }
    CFRelease(socials);

    return twitterUsername;
}

I've put the if to validate the "socials" array is nil because I was getting exception when trying to get "socials" array count. 我放置了if来验证“社交”数组为nil,因为在尝试获取“社交”数组计数时出现异常。

I've tried this code in the simulator and in a real device with contacts which twitter info is filled but the "socials" array I get is always nil. 我已经在模拟器和真实设备中尝试了此代码,并在其中填充了Twitter信息的联系人,但我得到的“社交”数组始终为零。 I'm getting the wrong property from the record? 我从记录中获得了错误的财产? Any help? 有什么帮助吗?

Thank you in advance 先感谢您

your function works for me almost as-is, aside from needing to bridge cast the twitter username (which is just an ARC thing). 除了需要桥接强制转换twitter用户名(这只是ARC的事情)之外,您的函数几乎对我一样有效。 how are you accessing the address book? 您如何访问通讯录?

when i use this code to call your function: 当我使用此代码调用您的函数时:

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);

CFIndex contactCount = ABAddressBookGetPersonCount(addressBook);

for( int i = 0; i<contactCount; i++ )
{
    ABRecordRef ref = CFArrayGetValueAtIndex(people, i);
    NSString *twitterHandle = [self getTwitterUsernameFromRecord:ref];
    if (twitterHandle) {
        NSLog(@"record %d has twitter name %@", i, twitterHandle);
    }
}

i get this console output: 我得到此控制台输出:

2012-06-26 12:09:40.864 AddressBookTest[2246:707] record 57 has twitter name alexshepard

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM