0

编辑:还没有解决方法,但 Facebook 在自己的 SDK 中似乎已经弃用了使用系统 FB 登录。我尝试了最新的 SDK,它完全忽略了系统登录。不幸的是,我不能使用最新的 SDK,因为尽管使用了“旧版”设置,但它仍然返回一个不能给我所需的令牌。不过,看看他们如何绕过系统设置可能会很有用!

原Q

我有一个使用 Facebook 进行身份验证的应用程序。

我正在使用 Facebook SDK(3.13.1 - 最后一个 pre api v2 版本)

如果我尝试进行身份验证,只要我没有在系统设置中设置 FB 帐户,一切正常。该应用程序启动 FB 移动应用程序或移动 Safari 以登录并提示我授予必要的权限。

一旦有系统帐户设置,它就会静默失败。我在两次运行之间删除了应用程序,以确保不会持续缺少权限。如果我运行 SDK 附带的一些 FB 示例应用程序,它们会提示许可就好了,所以这显然是我的应用程序或 Facebook 应用程序的配置有问题。

我看过这两个类似的问题:

Facebook 登录 - 如果存在用户帐户(并且未安装应用程序)登录失败

当 Facebook 帐户已在系统偏好设置中配置时出现 Facebook 登录错误

但两者都没有提供适合我情况的答案。(例如,我确实在 Facebook 应用程序中配置了捆绑标识符)

我尝试使用我自己的自定义登录代码(基于 Facebook 指南)和使用 FBLoginView,两者都具有相同的效果。

我也尝试过使用 ACAccountStore 直接向系统存储发出请求,效果相同。有任何想法吗?

我将包含一些代码,但我相当肯定代码不是问题 - 如果未配置系统 FB 帐户,一切正常:(我在这里更改了我的 FacebookAppIDkey)

ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options = @{  ACFacebookAppIdKey: @"123",
                            ACFacebookPermissionsKey: @[@"public_profile", @"user_likes", @"user_friends", @"email"],
                            ACFacebookAudienceKey: ACFacebookAudienceFriends};

[accountStore requestAccessToAccountsWithType:facebookType options:options completion:^(BOOL granted, NSError *error) {
    if (granted) {
        NSArray *accounts = [accountStore
                accountsWithAccountType:facebookType];
        id facebookAccount = [accounts lastObject];
        DLog(@"%@", facebookAccount);
    }
    else
    {
        if (error)
        {
            DLog(@"%@", error.localizedDescription);
        }
    }
}];

使用 SDK:

[FBSession openActiveSessionWithReadPermissions:@[@"public_profile", @"user_likes", @"user_friends", @"email"]
                                       allowLoginUI:YES
                                  completionHandler:
                                          ^(FBSession *session, FBSessionState state, NSError *error) {

                                              AppDelegate* appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
                                              [appDelegate sessionStateChanged:session state:state error:error];
                                              if ([FBSession activeSession].isOpen) {
                                                  __typeof__(self) strongSelf = weakSelf;
                                                  strongSelf.facebookToken = session.accessTokenData.accessToken;
                                                  [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
                                                  {
                                                      completion(error,(NSDictionary *)result);
                                                  }];
                                              } else {
                                                  completion(error,nil);
                                              }

                                          }];
4

1 回答 1

0

The problem may is because you are already logged in but doesn't have an access token. So you have to check the condition if(appDelegate.session.state == FBSessionStateCreatedTokenLoaded) .

Hope the following code will help.

VNSAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];




if (!appDelegate.session.isOpen) {
    // create a fresh session object
    appDelegate.session = [[FBSession alloc] init];

    // if we don't have a cached token, a call to open here would cause UX for login to
    // occur; we don't want that to happen unless the user clicks the login button, and so
    // we check here to make sure we have a token before calling open
    if (appDelegate.session.state == FBSessionStateCreatedTokenLoaded) {
        // even though we had a cached token, we need to login to make the session usable

        [[appDelegate.session initWithPermissions:appDelegate.permissions]openWithCompletionHandler:^(FBSession *session,
                                                         FBSessionState status,
                                                         NSError *error) {
            // we recurse here, in order to update buttons and labels
            [self updateView];
        }];

    }
于 2014-06-18T13:56:19.837 回答