【问题标题】:iOS Facebook Publish Feed Dialog doesn't show up after authenticationiOS Facebook 发布提要对话框在身份验证后不显示
【发布时间】:2012-01-17 23:34:57
【问题描述】:

感谢阅读。

我通过Facebook iOS SDK Feed Dialog issue after authentication 但徒劳无功。

什么有效: 我使用 Facebook iOS Tutorial 和 Feed 对话框创建了一个示例“Hello, World”应用程序。本教程运行良好,我可以在我的提要中看到该帖子。

问题: 当我将此代码集成到我的应用程序中时,当控件返回到我的应用程序时,我在身份验证后看不到提要对话框。

控制流程: 我有一个UIImagePickerController 显示一个正在拍照的相机,然后显示UIAlertView 表示正在上传图像,显示一个UIAlertView 显示从服务器返回的结果,最后显示一个UIActionSheet 到显示不同的分享选项(分享到 Facebook、Twitter 等)。

当用户点击“分享到 Facebook”时,会调用以下 selector


- (void) initFacebook
{
    //Init Facebook
    facebook = [[Facebook alloc] initWithAppId:@"308136969223987" andDelegate:self];

    //Check for access_token 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if([defaults objectForKey:@"FBAccessTokenKey"]
       && [defaults objectForKey:@"FBExpirationDateKey"]) {
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }

    if(![facebook isSessionValid]) {
        [facebook authorize:nil];
    }

    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  kAppId, @"app_id",
  @"https://developers.facebook.com/docs/reference/dialogs/", @"link",
  @"http://fbrell.com/f8.jpg", @"picture",
  @"Facebook Dialogs", @"name",
  @"Reference Documentation", @"caption",
  @"Using Dialogs to interact with users.", @"description",
  @"Facebook Dialogs are so easy!",  @"message",
  nil];

  [_facebook dialog:@"feed" andParams:params andDelegate:self];  
}

这会启动 Facebook 应用进行身份验证,然后使用 UIImagePickerController 再次打开我的应用,但不会显示 Feed Dialog

有人可以帮帮我吗?

【问题讨论】:

    标签: ios facebook facebook-ios-sdk


    【解决方案1】:

    您必须在 FBSessionDelegate 的 -(void)fbDidLogin 中再次手动调用所需的方法,在您的情况下为 -(void)initFacebook。

    另外,为了让这个更通用,我在 [facebook authorize:nil] 之后做了一个小技巧;我设置了一个挂起的目标和选择器来存储登录后需要执行的挂起命令。像这样的...

    id fbPendingTarget;
    SEL fbPendingAction;
    
        if (![delegate.facebook isSessionValid]) {
            [delegate.facebook authorize:nil]; // Facebook app or Safari login, mustitask
    
            // Save pending actions
            delegate.fbPendingTarget = self;
            delegate.fbPendingAction = @selector(facebookButtonPressed);
    
            [self retain]; // *** hold on to self if you will release self and is using fbPendingTarget as self
        } 
    

    当委托触发时,它会运行您的待处理操作...

    - (void)fbDidLogin {
        [self storeAuthData:[facebook accessToken] expiresAt:[facebook expirationDate]];
        [self apiFQLIMe]; // request user info
    
        // perform any pending actions after login
        if ([fbPendingTarget respondsToSelector:fbPendingAction]) {
            [fbPendingTarget performSelector:fbPendingAction];
        }
        self.fbPendingTarget = nil;
        self.fbPendingAction = nil;
    
        [self release]; // *** let go of self after we finished everything
    }
    

    这有助于在登录后执行您需要的任何待处理操作。并且不要忘记在执行或登录失败后清除它们。

    -(void)fbDidNotLogin:(BOOL)cancelled {
        UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Cannot Connect"
                                                             message:@"There was an error while connecting to Facebook. Please try Again."
                                                            delegate:self
                                                   cancelButtonTitle:@"OK"
                                                   otherButtonTitles:nil] autorelease];
        [alertView show];
    
        self.fbPendingTarget = nil;
        self.fbPendingAction = nil;
    
        [self release]; // *** let go of self after we finished everything
    }
    

    希望这会有所帮助:)

    编辑:我发现将fbPendingTarget 设置为应用程序生命周期中存在的某个对象(如appDelegate)比设置为self 更好。这是因为在许多情况下,您的self 是在用户向 Facebook 发送帖子后立即发布的视图。向已发布的对象发送消息会使您的应用崩溃。

    解决此问题的一种肮脏方法是在发送之前明确使用[self retain],然后在fbPendingAction 中使用[self release]。但这将保留到那时应该在 self 中释放的所有其他内容。我只是发现设置为appDelegate 更容易,因为您要做的大多数操作只是显示一个警报。为了安全起见,我将编辑代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      相关资源
      最近更新 更多