How do I set LoginBehaviour when using react-native-fbsdk LoginManager?

你说的曾经没有我的故事 提交于 2019-12-05 16:10:17

I had a similar issue and managed to make it work by setting the login behavior as follows:

LoginManager.setLoginBehavior('NATIVE_ONLY'); 

Facebook documentation is poor even in react-native-fbsdk GitHub repo about the subject :(

EDIT: There is a catch by using natie_only behavior. User must have FB installed at this phone, otherwise the FB SDK fails silently. To address that I decided to launch the WEB_ONLY behavior in case the native_only fails. My example is tested for Android, not iOS yet.

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

EDIT EDIT: I published an article on how to use Facebook SDK in React Native where I mention more stuff (i.e. how to perform graph requests). Check it out if you need more info on the subject.

I had the same confusion. I found the information in the FBSDK source. There is a different list for Andtoid and iOS. I ended up using a cross platform version of @jeevium answer above.

// 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';
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!