使用react-native-fbsdk LoginManager时如何设置LoginBehaviour?

时间:2016-10-03 15:19:48

标签: react-native react-native-fbsdk

如果我使用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/

2 个答案:

答案 0 :(得分:6)

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

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']);
}

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

答案 1 :(得分:0)

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

// 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';