2

我的 iOS 应用程序面临以下问题:

  1. 在发布到用户朋友的墙上时,使用 iOS 应用程序我收到错误“ com.facebook.sdk error 5”。

  2. 每次我检查“ FBSession.activeSession.isOpen”时,该应用程序都会要求登录。

之前一切正常。但是由于我已经更改了我的应用程序 ID(因为现在我必须使用不同的 Facebook 应用程序)。我得到了错误。

我对此进行了一些研究并找到了一些解决方案。我都试过了,但没有任何效果。

我编写了以下代码来登录 Facebook:

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
comingfromFB = YES;
NSArray *permissions = [[NSArray alloc] initWithObjects:
                        @"friends_birthday",
                        nil];

return [FBSession openActiveSessionWithReadPermissions:permissions
                                               allowLoginUI:allowLoginUI
                                          completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                                              [self sessionStateChanged:session state:state error:error];
                                          }];
}

以及以下用于发布故事的代码:

if ([FBSession.activeSession.permissions
         indexOfObject:@"publish_actions"] == NSNotFound) {
        // No permissions found in session, ask for it
        [FBSession.activeSession
         reauthorizeWithPublishPermissions:
         [NSArray arrayWithObject:@"publish_actions"]
         defaultAudience:FBSessionDefaultAudienceFriends
         completionHandler:^(FBSession *session, NSError *error) {
             if (!error) {
                 // If permissions granted, publish the story
                 [self publishStory];
             }
         }];
    } else {
        // If permissions present, publish the story
        [self publishStory];
}

当我运行应用程序时,我收到以下错误:

Error: HTTP status code: 403

2013-02-05 14:36:47.109 <appname>[1705:c07] Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0xaa9af20 {com.facebook.sdk:ParsedJSONResponseKey={
    body =     {
        error =         {
            code = 200;
            message = "(#200) The user hasn't authorized the application to perform this action";
            type = OAuthException;
        };
    };
    code = 403;
}, com.facebook.sdk:HTTPStatusCode=403}

请帮助我了解我哪里出错了。

我还添加了一些键值对到NSUserDefaults. 这可能是一个可能的原因吗?

谢谢。

4

1 回答 1

1

当您第一次使用发布按钮显示您的视图时,您可能想要检查会话是否打开并根据会话状态设置您的发布按钮标题。假设您的按钮最初具有“发布”标题:

[FBSession openActiveSessionWithAllowLoginUI: NO];

if (![FBSession.activeSession isOpen]) 
{
    [self setSendButtonTitle:NSLocalizedString(@"Log in",@"")];
}

然后将以下代码添加到您的发布按钮操作:

- (void) send: (UIButton*) sender
{
    if (![FBSession.activeSession isOpen]) 
    {
        [FBSession openActiveSessionWithPublishPermissions: [NSArray arrayWithObjects: @"publish_stream", nil]
                                       defaultAudience: FBSessionDefaultAudienceEveryone
                                          allowLoginUI: YES

                              completionHandler: ^(FBSession *session,
                                                  FBSessionState status,
                                                  NSError *error) 
                              {
                                  if (error)
                                  {
                                      NSLog(@"error");
                                  } 
                                  else 
                                  {
                                      [FBSession setActiveSession:session];
                                      [self setSendButtonTitle: NSLocalizedString(@"Post",@"")];
                                  }
                              }];

        return;
    }

    // here comes the code you use for sending the message
}

因此,如果用户按下发送并且应用程序未授权,它将执行登录操作并将您的发布按钮标题从“登录”更改为“发布”。然后用户将能够通过再次按下按钮来发布消息。

希望能帮助到你

于 2013-02-05T10:46:54.307 回答