在facebook上发布状态

时间:2013-10-08 09:21:09

标签: ios objective-c facebook

我正在实施一个用户可以发布状态的应用。如果用户在他/她的设备中默认登录,则以下代码将起作用

  if(!self.accountStore)
    self.accountStore = [[ACAccountStore alloc] init];
if(NSClassFromString(@"SLComposeViewController") != nil)
{
    ACAccountType *facebookTypeAccount = [self.accountStore 
    accountTypeWithAccountTypeIdentifier:@"com.apple.facebook"];
    NSLog(@"facebookTypeAccount..:%@",facebookTypeAccount);
    NSArray *accounts = [self.accountStore accountsWithAccountType:facebookTypeAccount];
    if (facebookTypeAccount) {
        if ([accounts respondsToSelector:@selector(count)]) {
            [self.accountStore requestAccessToAccountsWithType:facebookTypeAccount
                                                       options:@{ACFacebookAppIdKey: @"214267482030378", ACFacebookPermissionsKey: @[@"email"]}
                                                    completion:^(BOOL granted, NSError *error) {
                                                        if(granted){
                                                            NSArray *accounts = [self.accountStore accountsWithAccountType:facebookTypeAccount];
                                                            _facebookAccount = [[accounts lastObject]retain];
                                                        }
                                                        else
                                                        {
                                                            NSLog(@"Failed, Error: %@", error);
                                                        }
                                                    }];
        }
        else
        {
            [self openSession];
        }
    }
} 

否则控制将转到此方法

- (void)openSession{
NSArray *permissions = [[NSArray alloc] initWithObjects:
                        @"publish_stream",@"email",
                        nil];
[FBSession openActiveSessionWithPublishPermissions:permissions
                                   defaultAudience:FBSessionDefaultAudienceEveryone
                                      allowLoginUI:YES
                                 completionHandler:^(FBSession *session, FBSessionState state, NSError *error)
 {
     [self sessionStateChanged:session state:state error:error];

 }];

}

但是登录时我需要在Facebook上发送状态。如果我尝试使用这种方法

   -(void)poststatus{
[[FBSession activeSession] reauthorizeWithPublishPermissions:@[ @"publish_stream" ]  
defaultAudience:FBSessionDefaultAudienceEveryone completionHandler:^(FBSession 
*authSession, NSError *authError) {
    // If auth was successful, create a status update FBRequest
    if (!authError) {
        FBRequest *postRequest = [FBRequest requestForPostStatusUpdate:@"Hello, world!"];
        [postRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
            // TODO: Check for success / failure here
        }];
    }
}];

}

然后它给出错误失败,错误:错误域= com.apple.accounts代码= 8“操作无法完成。(com.apple.accounts错误8.)”请帮助我,如果有人有关于这个概念的一些想法。

提前致谢。

1 个答案:

答案 0 :(得分:1)

要求:

@[@"publish_stream", @"publish_actions"]

而不仅仅是:

@[ @"publish_stream" ]

了解更多: https://developers.facebook.com/docs/reference/login/extended-permissions/

publish_stream  The publish_stream permission is required to post to a Facebook Page's timeline. For a Facebook User use publish_actions.

此外,您正在使用已弃用的功能。试试:

[FBSession requestNewPublishPermissions:defaultAudience:completionHandler:];