objective-c - 获取 Facebook 个人资料名称和图片 IOS 5.1

标签 objective-c ios facebook-requests

我无法同时获取 Facebook 名称和图片。每个请求我只能接受其中一个。

-(void)fbDidLogin{
    // Save the access token key info.
    [self saveAccessTokenKeyInfo];

    // Get the user's info.
    [facebook requestWithGraphPath:@"me/?fields=first_name,picture" andDelegate:self];

}

-(void)request:(FBRequest *)request didLoad:(id)result{

    if ([result isKindOfClass:[NSArray class]]) {

        result = [result objectAtIndex:0];

        if ([result objectForKey:@"first_name"]) {
            [lblUser setText:[NSString stringWithFormat:@"Welcome %@!", [result objectForKey:@"first_name"]]];
            // Show the publish button.
            [btnPublish setHidden:NO];
        }
    }
    else {
       imageView.image = [UIImage imageWithData:result];
    }      
}

我得到的错误

__NSCFDictionary length]: unrecognized selector sent to instance
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary length]: unrecognized selector sent to instance 

如果我提出两个请求

-(void)fbDidLogin{
    // Save the access token key info.
    [self saveAccessTokenKeyInfo];

    // Get the user's info.
    [facebook requestWithGraphPath:@"me/picture" andDelegate:self];
    [facebook requestWithGraphPath:@"me" andDelegate:self];
}

并使用相同的 -(void)request:(FBRequest *)request didLoad:(id)result 我得到的错误是

[__NSCFDictionary length]: unrecognized selector sent to instance

那么我如何请求获取 Facebook 名称和图片以及如何处理传入数据?

最佳答案

我修好了,下面的代码可以很好地获取 facebook 名称和个人资料图片

-(void)request:(FBRequest *)request didLoad:(id)result{
    // With this method we’ll get any Facebook response in the form of an array.
    // In this example the method will be used twice. Once to get the user’s name to
    // when showing the welcome message and next to get the ID of the published post.
    // Inside the result array there the data is stored as a NSDictionary.    


    if ([result isKindOfClass:[NSData class]])
    {
        NSLog(@"Profile Picture");
        imageView.image = [UIImage imageWithData:result];
        //[profilePicture release];
        //profilePicture = [[UIImage alloc] initWithData: result];
    }


  if ([result isKindOfClass:[NSArray class]]) {
        // The first object in the result is the data dictionary.

        result = [result objectAtIndex:0];


    }

    if ([result isKindOfClass:[NSDictionary class]]) {
        // If the current result contains the "first_name" key then it's the user's data that have been returned.
        // Change the lblUser label's text.
        [lblUser setText:[NSString stringWithFormat:@"Welcome %@!", [result objectForKey:@"first_name"]]];
        // Show the publish button.
        [btnPublish setHidden:NO];
    }

}

关于objective-c - 获取 Facebook 个人资料名称和图片 IOS 5.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11144568/

相关文章:

iphone - UIAlertView 中的 UITextView 大文本

ios - 为什么我的后退按钮在 iOS 7 上不起作用?

ios - iOS:每2分钟在后台播放一次短声音

ios - 如何强制获取核心数据以上传到 Azure 移动服务?

iphone - Fb 请求对话框错误地返回 nil - IOS Facebook

facebook - 使用 facebook API 接受好友请求

ios - ffmpeg ios : Getting frame to UIImage, 在 YUV420 到 RGB 转换时失败

iphone - 在 UIImage 周围放置圆形框架的最简单方法

ios - Twitter Api 如何将 unicode 转换为字符串?