如何仅从结果集中获取用户名

时间:2012-02-09 10:58:19

标签: iphone ios addressbook ios5

如何在选择联系人的Twitter详细信息时,仅从地址簿中获取用户名值

if ( property == kABPersonSocialProfileProperty) {

        ABMultiValueRef multi = ABRecordCopyValue(person, property);


        for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++) {

            CFStringRef socialLabel = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(multi, identifier));

            CFStringRef social = ABMultiValueCopyValueAtIndex(multi, i);

            NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(multi, i);        

            NSString*twitterdetails = [personaddress valueForKey:(NSString *)kABPersonSocialProfileServiceTwitter];
            NSLog(@"%@ twitterdetails",twitterdetails);  


            NSString *aString = (NSString *)social;


            NSLog(@"%@ aString",aString);
        }

我得到的结果如下

{
    service = twitter;
    url = "http://twitter.com/manoas2136";
    username = manoas2136;
} aString

请告诉我如何只获取上述结果集中的用户名

2 个答案:

答案 0 :(得分:3)

试试这段代码

  NSDictionary* personalDetails = [NSDictionary dictionaryWithDictionary:(NSDictionary*)ABMultiValueCopyValueAtIndex(multi, i)];
  NSString* aString = [personalDetails valueForKey:@"username"];
  NSLog(@"%@ aString",aString);

结果manoas2136 aString

您将NSDictionary值转换为字符串,因此它将NSDictionary的所有结果分配到NSString,因此您只使用valueForKey提取用户名:

答案 1 :(得分:0)

我试过这种方式从iphone联系人列表中获取推特地址。它工作正常..

ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonSocialProfileProperty);

for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++) 
{

    NSDictionary* personalDetails = [NSDictionary dictionaryWithDictionary:(NSDictionary*)ABMultiValueCopyValueAtIndex(multi, i)];
    NSLog(@"%@",personalDetails);

    NSString* aString1 = [personalDetails valueForKey:@"username"];
    NSLog(@"%@ aString",aString1);
    Twitter.text = [personalDetails valueForKey:@"username"];
}
//get Twitter Address end....