在iOS 6中使用带有Facebook的SLRequest

时间:2012-09-24 16:27:22

标签: iphone objective-c facebook acaccount social-framework

我目前正在尝试使用SLRequest将状态发布到Facebook,这是我的代码:

ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    NSLog(@"0");
    [accountStore requestAccessToAccountsWithType:accountType options:@{ACFacebookAppIdKey : @"00000000000", ACFacebookPermissionsKey : @"publish_stream", ACFacebookAudienceKey : ACFacebookAudienceFriends} completion:^(BOOL granted, NSError *error) {
        if(granted) {
            NSLog(@"1");
            NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
            NSLog(@"2");
            if ([accountsArray count] > 0) {
                NSLog(@"3");
                ACAccount *facebookAccount = [accountsArray objectAtIndex:0];
                NSLog(@"4");
                SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                                requestMethod:SLRequestMethodPOST
                                                                          URL:[NSURL URLWithString:@"https://graph.facebook.com/me/feed"]
                                                                   parameters:[NSDictionary dictionaryWithObject:post forKey:@"message"]];
                NSLog(@"5");

                [facebookRequest setAccount:facebookAccount];
                NSLog(@"6");

                [facebookRequest performRequestWithHandler:^(NSData* responseData, NSHTTPURLResponse* urlResponse, NSError* error) {
                    NSLog(@"%@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
                }];


            }
        }
    }];

然而,我所拥有的代码不想过去if(granted){,但我不知道为什么我所做的不起作用。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:4)

在我的情况下,为了解决这个问题,我在Facebook上的APP属性中注册了Bundle ID。

在Facebook上编辑您的APP并查找“选择您的应用与Facebook集成的方式”,并在“原生iOS应用”中的“iOS Bundle ID”上注册您项目的Bundle ID。

如果不是您的情况,请尝试阅读错误消息:

if(granted) {
    ...
}
else {
    NSLog([NSString stringWithFormat:@"%@", error.localizedDescription]);
}

答案 1 :(得分:0)

如果error.code == 6,那么如果用户尚未从“设置”应用中登录Facebook,则会显示错误消息。