【问题标题】:Getting twitter user from Address Book从地址簿获取 twitter 用户
【发布时间】:2012-06-28 03:48:08
【问题描述】:

我正在尝试从通讯录中的联系人获取 Twitter 用户名,但我做不到

我正在使用我发现“谷歌搜索”的这段代码:

- (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;
}

我使用 if 来验证“socials”数组是否为零,因为在尝试获取“socials”数组计数时遇到异常。

我已经在模拟器和带有联系人的真实设备中尝试了此代码,其中填充了 twitter 信息,但我得到的“社交”数组始终为零。我从记录中得到了错误的财产?有什么帮助吗?

提前谢谢你

【问题讨论】:

    标签: iphone objective-c twitter abaddressbook


    【解决方案1】:

    除了需要桥接 twitter 用户名(这只是 ARC 的事情)之外,您的功能几乎可以按原样为我工作。您是如何访问通讯录的?

    当我使用此代码调用您的函数时:

    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);
        }
    }
    

    我得到这个控制台输出:

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

    【讨论】:

    • 好吧,这很尴尬... :D 我对“getTwitterUsernameFromRecord”方法太着迷了,以至于我没有注意前面的代码。我和你做同样的事情来获取联系人,但就在 ABAddressBookGetPersonCount(addressBook); 之后。我正在发布地址簿(使用 CFRelease(addressBook)),这导致了错误。非常感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 2016-10-12
    • 1970-01-01
    • 2011-05-06
    相关资源
    最近更新 更多