我目前只使用 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 并且在解析数据时发生了一些事情。我注意到 SO 上有几个关于相同错误代码的其他线程,但到目前为止还没有运气。我的猜测是 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.)