【问题标题】:New Facebook SDK FBSession sessionOpenWithPermissions新的 Facebook SDK FBSession sessionOpenWithPermissions
【发布时间】:2012-08-06 07:45:38
【问题描述】:

我一直在尝试使用 Facebook 开发 iOS 应用程序,但我是新手。所以我一直在努力做

使用 Facebook 登录,按照 Facebook 上的教程并尝试实现它。

但是我遇到过,[FBSession sessionOpenWithPermissions] 找不到。当我运行

应用程序,它将强制关闭并说出该错误。构建项目时会显示警告

FBSession中找不到sessionOpenWithPermission的黄色感叹号

教程过时了?如果是,那么新的 Facebook SDK 的新代码是什么

sessionOpenWithPermission ?

【问题讨论】:

  • 我认为这是有效的——我在 Facebook 示例中遇到了同样的问题。
  • 这显然是一个文档不匹配问题。我建议查看 SDK 附带的“美味”示例应用程序以获取最新文档。
  • 文档教程和示例不匹配。你应该改用- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;

标签: iphone facebook login


【解决方案1】:

试试这个代码

 account = [[ACAccountStore alloc] init];
    accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    arrayOfAccounts = [account accountsWithAccountType:accountType]; 


    appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];
    chk=appDelegate.chk_login;

    if (!appDelegate.session.isOpen) {
        // create a fresh session object
        appDelegate.session = [[FBSession alloc] init];
        if (appDelegate.session.state == FBSessionStateCreatedTokenLoaded) {
            // even though we had a cached token, we need to login to make the session usable
            [appDelegate.session openWithCompletionHandler:^(FBSession *session,
                                                             FBSessionState status,
                                                             NSError *error) {
                // we recurse here, in order to update buttons and labels

            }];
        }
    }

【讨论】:

  • 由于FBSession 具有activeSession 静态公共变量,您不必将会话保存在应用委托中。
【解决方案2】:

也许这段代码对你有帮助,把它放在你的 AppDelegate.m 类中

- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen: {
            self.loggedinVCController = [[LoggedinVC alloc] initWithNibName:@"LoggedinVC" bundle:nil];
            self.navController = [[UINavigationController alloc]initWithRootViewController:self.loggedinVCController];
            self.window.rootViewControlle`enter code here`r = self.navController;

        }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            // Once the user has logged in, we want them to
            // be looking at the root view.
            [self.navController popToRootViewControllerAnimated:NO];

            [FBSession.activeSession closeAndClearTokenInformation];

            self.viewController = [[SampleViewController alloc] initWithNibName:@"SampleViewController" bundle:nil];
            self.window.rootViewController = self.viewController;

            break;
        default:
            break;
    }

    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:error.localizedDescription
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return [FBSession.activeSession handleOpenURL:url];
}
- (void)openSession
{
    [FBSession openActiveSessionWithReadPermissions:nil
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session,
       FBSessionState state, NSError *error) {
         [self sessionStateChanged:session state:state error:error];
     }];
}

【讨论】:

    【解决方案3】:

    Facebook iOS SDK 3.0 Login Tutorial Issue with FBSession 可能重复

    //REPLACE
    [FBSession sessionOpenWithPermissions:nil
                        completionHandler: ^(FBSession *session, FBSessionState state, NSError *error) {
                            [self sessionStateChanged:session state:state error:error];
                        }];
    
    //WITH
    [FBSession openActiveSessionWithPermissions:nil
                                   allowLoginUI:YES
                              completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                                  [self sessionStateChanged:session state:state error:error];
                              }];
    

    【讨论】:

      【解决方案4】:

      它会打开 facebook seesion 并选择性地显示登录 ux

      - (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
          NSArray *permissions = [[NSArray alloc] initWithObjects:
                                  @"email",
                                  @"user_likes",
                                  nil];
          return [FBSession openActiveSessionWithReadPermissions:permissions
                                                    allowLoginUI:allowLoginUI
                                               completionHandler:^(FBSession *session,
                                                                   FBSessionState state,
                                                                   NSError *error) {
                                                   [self sessionStateChanged:session
                                                                       state:state
                                                                       error:error];
                                               }];}
      

      【讨论】:

        【解决方案5】:

        只需将 cmets 中的@Stas Zhukovskiy 答案复制到答案框中:

        文档教程和示例不匹配。你应该使用 - (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;反而。 – Stas Zhukovskiy

        【讨论】:

          【解决方案6】:

          您也可以使用 Sharekit 它很容易实现,也支持其他社交网络。 sharekit

          Share kit tutorial

          【讨论】:

            【解决方案7】:

            App Delegate 中缺少某些行代码。只需检查那个。之后,您将在调用 Facebook 方法时检查打开会话和关闭会话。

            【讨论】:

              猜你喜欢
              • 2012-10-25
              • 2015-03-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-12
              • 1970-01-01
              • 2012-09-26
              相关资源
              最近更新 更多