5

如果我使用 react-native-fbsdk 中的 LoginButton ,则 LoginBehaviour 似乎是“本地的”,因为 SDK 与已安装的 FB 应用程序通信,看到我已经授予权限并且只是登录而不显示任何对话框等。

当我使用 LoginManger.logInWithPublishPermissions() 机制时,我总是被带到浏览器和一个显示我已经授予我的应用程序权限的屏幕。我想我可以通过设置登录行为来改变它,但我不知道如何成功地做到这一点。这是我尝试过的

import { GraphRequest, GraphRequestManager, LoginManager, LoginBehaviorIOS } from 'react-native-fbsdk';
LoginManager.setLoginBehavior(LoginBehaviorIOS);

//ERROR: Argument 0 (FBSDKLoginBehaviour) of FBLoginManager: must not be null

然后我尝试了这个:

LoginManager.setLoginBehavior('native');

// No error, but still gives me the same behaviour.

LoginButton 和 LoginManager 的行为何时不同?使用 LoginManager 时如何设置登录行为,使其像 LoginButton 一样工作?

我已将所有代码添加到 AppDelegate.m 文件以及入门指南中包含的所有其他说明:https ://developers.facebook.com/docs/ios/getting-started/

4

2 回答 2

11

我有一个类似的问题,并设法通过设置登录行为使其工作,如下所示:

LoginManager.setLoginBehavior('NATIVE_ONLY'); 

即使在 react-native-fbsdk GitHub repo中关于该主题的 Facebook 文档也很差:(

编辑: 使用 natie_only 行为有一个问题。用户必须在此手机上安装 FB,否则 FB SDK 会静默失败。为了解决这个问题,我决定在 native_only 失败的情况下启动 WEB_ONLY 行为。我的示例已针对 Android 进行了测试,尚未针对 iOS 进行测试。

let result;
try {
  LoginManager.setLoginBehavior('NATIVE_ONLY');
  result = await LoginManager.logInWithReadPermissions(['public_profile', 'email']);
} catch (error) {
  LoginManager.setLoginBehavior('WEB_ONLY');
  result = await LoginManager.logInWithReadPermissions(['public_profile', 'email']);
}

编辑编辑:我发表了一篇关于如何在 React Native 中使用 Facebook SDK的文章,其中我提到了更多内容(即如何执行图形请求)。如果您需要有关该主题的更多信息,请查看它。

于 2017-11-13T15:27:28.437 回答
1

我也有同样的困惑。我在 FBSDK 源代码中找到了信息。Andtoid 和 iOS 有不同的列表。我最终使用了上面@jeevium 答案的跨平台版本。

// something like this
LoginManager.setLoginBehavior(Platform.OS === 'ios' ? 'native' : 'NATIVE_ONLY');

/**
 * Indicate how Facebook Login should be attempted on Android.
 */
export type LoginBehaviorAndroid =
    // Attempt login in using the Facebook App, and if that does not work fall back to web dialog auth.
    'native_with_fallback'|
    // Only attempt to login using the Facebook App.
    'native_only'|
    // Only the web dialog auth should be used.
    'web_only';

/**
 * Indicate how Facebook Login should be attempted on iOS.
 */
export type LoginBehaviorIOS =
    // Attempts log in through the native Facebook app.
    // The SDK may still use Safari instead.
    // See details in https://developers.facebook.com/blog/post/2015/10/29/Facebook-Login-iOS9/
    'native' |
    // Attempts log in through the Safari browser.
    'browser' |
    // Attempts log in through the Facebook account currently signed in through Settings.
    'system_account' |
    // Attempts log in through a modal UIWebView pop-up.
    'web';
于 2019-01-15T10:49:54.020 回答