Facebook原生登录无法打开Facebook应用,即使在设备上已安装?

6
我已经按照Facebook-iso-sdk 4.8.0的文档对iOS 9进行了每一个步骤,但是在我的应用程序中仍然无法执行“使用Facebook登录”的应用程序切换,即使Facebook应用程序已经安装。

如您所见,在下面的屏幕截图中,我已修改了info.plist,但仍无法使本地应用程序切换工作。

我还仔细检查了info.plist值的拼写错误。我可以保证它们是正确的。

这是我的代码:-

    if (![AppDelegate SharedInstance].login) {
        [AppDelegate SharedInstance].login = [[FBSDKLoginManager alloc] init];
    }
    [AppDelegate SharedInstance].login.loginBehavior = FBSDKLoginBehaviorNative;
    [[AppDelegate SharedInstance].login logInWithReadPermissions:@[@"public_profile",@"email",@"user_friends"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (error) {

        }
        else if (result.isCancelled)
        {
            // Handle cancellations
        }
        else
        {
            NSLog(@"result.grantedPermissions == %@",result.grantedPermissions);
            if (result.token)
            {
                [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, email, first_name, last_name"}]
                 startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                     if (!error) {
                         NSLog(@"fetched user:%@", result);
                         NSString *userImageURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", [result objectForKey:@"id"]];
                         [dictFacebookDetail addEntriesFromDictionary:result];
                         [dictFacebookDetail setObject:userImageURL forKey:@"profilepic"];
                         NSLog(@"facebook login result --- %@",dictFacebookDetail);
                         [self performSelectorInBackground:@selector(CheckFacebookUser:) withObject:dictFacebookDetail];
                     }
                 }];
            }
        }
    }];

我错过了什么?

Screen Shot of info.plist


请确认您是否在您的视图控制器中添加了委托。 - Anbu.Karthik
我遇到了同样的问题。有解决方法吗? - Femina
@Meenu,请尝试下面croigsalvador的答案,它可以正常工作。 - Pawan Joshi
我没有使用Pod,我正在使用SDK 4.8。我在哪里可以进行这些更改? - Femina
@Meenu:你应该使用它们,它们非常容易使用。Ray Wenderlich有一个很棒的教程。无论如何...只需在项目中搜索文本“-(void)logInWithBehavior:(FBSDKLoginBehavior)loginBehavior”。您将找到一个名为“FBSDKLoginManager.m”的文件。根据Croigsalvador的答案更改函数即可。 - Pawan Joshi
显示剩余3条评论
4个回答

9

我找到了一个解决方案,但你需要更改 FBSDKLogin pod 中的一些内容。我调试了 Pod 并意识到 Facebook 在 FBSDKServerConfiguration 类中询问应用程序的服务器配置。它返回一个 JSON,其中包含一些信息,以配置我们应用程序的 Pod。我发现默认情况下,JSON 返回以下字典:

 "ios_sdk_dialog_flows" =     {
        default =         {
            "use_native_flow" = 0;
            "use_safari_vc" = 1;
        };
        message =         {
            "use_native_flow" = 1;
        };
    };

默认情况下,use_native_flow为0,所以当它保存信息到userDefaults用于下一次应用程序启动时。 因此,当应用程序调用FBSDKLoginMananger登录方法并检查此方法中的loginBehaviour时,变量useNativeDialog返回NO。 因此,开关使用下一个case。case FBSDKLoginBehaviorBrowser:

- (void)logInWithBehavior:(FBSDKLoginBehavior)loginBehavior
{
.
.
...
  switch (loginBehavior) {
    case FBSDKLoginBehaviorNative: {
      if ([FBSDKInternalUtility isFacebookAppInstalled]) {
        [FBSDKServerConfigurationManager loadServerConfigurationWithCompletionBlock:^(FBSDKServerConfiguration *serverConfiguration, NSError *loadError) {
            BOOL useNativeDialog = [serverConfiguration useNativeDialogForDialogName:FBSDKDialogConfigurationNameLogin];
          if (useNativeDialog && loadError == nil) {
            [self performNativeLogInWithParameters:loginParams handler:^(BOOL openedURL, NSError *openedURLError) {
              if (openedURLError) {
                [FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorDeveloperErrors
                                   formatString:@"FBSDKLoginBehaviorNative failed : %@\nTrying FBSDKLoginBehaviorBrowser", openedURLError];
              }
              if (openedURL) {
                completion(YES, FBSDKLoginManagerLoggerAuthMethod_Native, openedURLError);
              } else {
                [self logInWithBehavior:FBSDKLoginBehaviorBrowser];
              }
            }];
          } else {
            [self logInWithBehavior:FBSDKLoginBehaviorBrowser];
          }
        }];
        break;
      }
      // intentional fall through.
    }
    case FBSDKLoginBehaviorBrowser: {
    .
    . 
    . 

}

通过代码我们可以看到,如果应用程序安装成功,则会进入以下if语句:if ([FBSDKInternalUtility isFacebookAppInstalled])

为了解决此问题,我已更改了此行代码。

BOOL useNativeDialog = [serverConfiguration useNativeDialogForDialogName:FBSDKDialogConfigurationNameLogin];

为了

 BOOL useNativeDialog = YES;

我知道这不是一个好的做法,如果我更新了这个Pod它会改变,但至少现在它可以工作,而且我现在需要它。 我猜我们可以在Facebook开发者管理网站中更改那个配置,但我还没有找到任何信息。


2
老兄...你真是我的救星。感谢你的努力,我非常感激。你已经帮我省去了很多麻烦。 - Pawan Joshi
更换Pod不是一个有效的解决方案。一定还有其他问题存在。 - Tudor
我知道,我觉得这一定是与Facebook开发者设置中的权限有关,但我找不到它。至少现在它正在工作。 - croigsalvador

8

Facebook已经改变了iOS9中的Facebook登录行为。

以下是来自Facebook博客发布的引用:

自从iOS 9发布以来,我们已经监控了250多个应用程序的数据和CTR超过6周。 SVC登录的点击率(CTR)优于应用切换登录的CTR,并且以3倍的速度改善了应用切换体验。这表明SVC体验今天对人们和开发者来说更好,并且在长期运营中可能是最佳解决方案。因此,最新的Facebook iOS SDK将使用SVC作为登录的默认体验。


1
  • (FBSDKServerConfiguration *)_defaultServerConfigurationForAppID:(NSString *)appID { // 当我们没有从服务器获得配置时,使用默认配置。 这使我们能够在等待服务器响应时在集中位置设置对话框集或其他任何内容的默认值。 static FBSDKServerConfiguration *_defaultServerConfiguration = nil; if (![_defaultServerConfiguration.appID isEqualToString:appID]) { // 绕过iOS 9+的本地对话框流程,因为它会产生一系列额外的确认对话框,导致不希望出现的额外摩擦。 NSOperatingSystemVersion iOS9Version = { .majorVersion = 9, .minorVersion = 0, .patchVersion = 0 };

1
typedef NS_ENUM(NSUInteger, FBSDKLoginBehavior)
{
  /*!
   @abstract This is the default behavior, and indicates logging in through the native
   Facebook app may be used. The SDK may still use Safari instead.
   */
  FBSDKLoginBehaviorNative = 0,
  /*!
   @abstract Attempts log in through the Safari or SFSafariViewController, if available.
   */
  FBSDKLoginBehaviorBrowser,
  /*!
   @abstract Attempts log in through the Facebook account currently signed in through
   the device Settings.
   @note If the account is not available to the app (either not configured by user or
   as determined by the SDK) this behavior falls back to \c FBSDKLoginBehaviorNative.
   */
  FBSDKLoginBehaviorSystemAccount,
  /*!
   @abstract Attemps log in through a modal \c UIWebView pop up

   @note This behavior is only available to certain types of apps. Please check the Facebook
   Platform Policy to verify your app meets the restrictions.
   */
  FBSDKLoginBehaviorWeb,
};

有很多方法可以访问Facebook登录。请根据喜好尝试使用其他可用方式。

FBSDKLoginManager *loginmanager = [[FBSDKLoginManager alloc] init];
        loginmanager.loginBehavior=FBSDKLoginBehaviorNative;

像这样...希望这能帮到你:)


1
它不执行应用程序切换,只是打开一个弹出窗口。我也尝试了FBSDKLoginBehaviorNative,但它也会在Safari中打开登录,而不是在安装的Facebook应用程序中打开。 - Pawan Joshi
使用loginbehaviernative = 0; - Kishore Kumar
我的 info.plist 中是否缺少了什么?你可以看一下我添加的截图。 - Pawan Joshi
让我们在聊天中继续这个讨论 - Kishore Kumar
你找到解决方案了吗? - Femina
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接