Objective c 本机Facebook/iOS6集成:尝试获取个人资料图片时出现Cocoa错误3840

Objective c 本机Facebook/iOS6集成:尝试获取个人资料图片时出现Cocoa错误3840,objective-c,ios6,facebook-ios-sdk,Objective C,Ios6,Facebook Ios Sdk,我目前只使用Social.framework(没有FacebookSDK),并开始请求和访问基本用户数据。以下是我所有的代码(您会注意到,在外部声明了一些属性)。就获得正确的权限而言,一切正常,但在询问用户的配置文件图片时,我得到以下输出错误: 2013-01-27 21:28:44.324 TestApp[9230:1a703] Account saved to accountStore 2013-01-27 21:28:45.002 TestApp[9230:1d603] (null) 20

我目前只使用Social.framework(没有FacebookSDK),并开始请求和访问基本用户数据。以下是我所有的代码(您会注意到,在外部声明了一些属性)。就获得正确的权限而言,一切正常,但在询问用户的配置文件图片时,我得到以下输出错误:

2013-01-27 21:28:44.324 TestApp[9230:1a703] Account saved to accountStore
2013-01-27 21:28:45.002 TestApp[9230:1d603] (null)
2013-01-27 21:28:45.003 TestApp[9230:1d603] Request error: The operation couldn’t be     completed. (Cocoa error 3840.)
代码如下:

- (IBAction)btnFbLoginPressed:(id)sender
{
ACAccountType *fbAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook];
NSArray *permissions = @[@"email"];
self.requestAccessOptions = @{ACFacebookAppIdKey:FB_API_KEY, ACFacebookPermissionsKey:permissions, ACFacebookAudienceKey:ACFacebookAudienceOnlyMe};

[self.accountStore requestAccessToAccountsWithType:fbAccountType options:self.requestAccessOptions completion:^(BOOL granted, NSError *e) {
    if (granted && e == nil) {

        NSArray *readPermissions = @[@"user_photos"];
        NSDictionary *readAcccessOptions = @{ACFacebookAppIdKey:FB_API_KEY, ACFacebookPermissionsKey:readPermissions, ACFacebookAudienceKey:ACFacebookAudienceOnlyMe};

        [self.accountStore requestAccessToAccountsWithType:fbAccountType options:readAcccessOptions completion:^(BOOL granted, NSError *e) {
            if (granted && e == nil) {

                NSArray *accounts = [self.accountStore accountsWithAccountType:fbAccountType];
                self.facebookAccount = [accounts lastObject];

                [self.accountStore saveAccount:self.facebookAccount withCompletionHandler:^(BOOL success, NSError *error) {
                    if (error != nil || !success) {
                        NSLog(@"Error saving account to accountStore: %@", error.localizedDescription);
                    } else {
                        NSLog(@"Account saved to accountStore");
                    }
                }];

                NSString *uid = [NSString stringWithFormat:@"%@", [[self.facebookAccount valueForKey:@"properties"] valueForKey:@"uid"]];
                NSString *url = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture", uid];
                NSURL *profilePictureURL = [NSURL URLWithString:url];

                SLRequest *profilePictureRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:profilePictureURL parameters:nil];
                profilePictureRequest.account = self.facebookAccount;

                [profilePictureRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *e)
                 {
                     NSDictionary *responseDataDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&e];
                     NSLog(@"%@", responseDataDictionary);
                     if (e != nil) {
                         NSLog(@"Request error: %@", e.localizedDescription);
                     } else {

                     }
                 }];

            } else {
                NSLog(@"Read permissions request error: %@", e.localizedDescription);
            }
        }];

    } else {
        NSLog(@"Basic permissions request error: %@", e.localizedDescription);
    }
}];
}
您可以看到reponseDataDictionary为null,并且在解析数据时发生了一些事情。我注意到了其他几个线程,所以关于相同的错误代码,但没有运气到目前为止。我的猜测是,要么1)我的Facebook代码有问题,要么2)我对数据的解析不正确。谢谢你的帮助

注意:我想坚持只使用社交/账户框架

更新:由于评论中的建议,略有修改

更改代码:

[profilePictureRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *e)
{
   NSLog(@"Request error value: %@", e.localizedDescription);

   NSError *jsonError = nil;
   NSDictionary *responseDataDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&jsonError];
   NSLog(@"Response data dictionary value: %@", responseDataDictionary);
   if (jsonError != nil) {
       NSLog(@"Serialization error: %@", jsonError.localizedDescription);
   } else {
       NSLog(@"Serialization successful");
   }
}];
输出:

2013-01-28 18:55:16.265 TestApp[9565:1ae03] Account saved to accountStore
2013-01-28 18:55:17.640 TestApp[9565:1b503] Request error value: (null)
2013-01-28 18:55:17.640 TestApp[9565:1b503] Response data dictionary value: (null)
2013-01-28 18:55:17.642 TestApp[9565:1b503] Serialization error: The operation couldn’t be completed. (Cocoa error 3840.)

profilePictureRequest返回的是图像,而不是JSON。使用
+(CIImage*)imageWithData:(NSData*)data
将响应转换为图像。

您应该记录传递到完成块的错误。目前,您正在通过调用JSONObjectWithDataThank覆盖它,这是一个很好的观点!不过这里没有什么有趣的东西——它只返回null。我已经用我的更改更新了这个问题。我猜我误读了文档,并希望得到带有图像URL的JSON格式的响应。。。无论如何,如果有人觉得这很有用,您也可以用相同的方式初始化UIImage:UIImage*image=[[[UIImage alloc]initWithData:data]autorelease];其中“data”是performRequestWithHandler中的NSData。@Svilen如果我想获取图像url而不是图像,如何?@AtifImran检查我问题中的代码示例-url始终是。@Svilen哦,对不起,可能是我问得不清楚。我想从profielPictureRequest(可能是json)获取图像url。因此,当我用url graph.facebook.com/uid/picture打电话时,我得到的是图像url,而不是图像数据。可能吗?Thanks@AtifImran我认为你应该问一个新问题,评论不是获得问题答案的最佳方式。