react-native-fbsdk loginbutton not work well

2k views Asked by At

I am using react-native-fbsdk 0.4.0 and followed the steps from https://github.com/facebook/react-native-fbsdk. I have tried the native Object-C testing app after adding the native FBSDK and the FB login button works without problems.

However, I got the same issue as this post React Native Facebook Login using official fbsdk.

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';

const FBSDK = require('react-native-fbsdk');
const {
  LoginButton,
  AccessToken
} = FBSDK;

var Login = React.createClass({
  render: function() {
    return (
      <View>
        <LoginButton
          publishPermissions={["publish_actions"]}
          onLoginFinished={
            (error, result) => {
              if (error) {
                alert("login has error: " + result.error);
              } else if (result.isCancelled) {
                alert("login is cancelled.");
              } else {
                AccessToken.getCurrentAccessToken().then(
                  (data) => {
                    alert(data.accessToken.toString())
                  }
                )
              }
            }
          }
          onLogoutFinished={() => alert("logout.")}/>
      </View>
    );
  }
});

AppRegistry.registerComponent('fbsdk_rn', () => Login);

Really appreciate any help. Thanks.

UPDATE: just fix my problem and hope can inspire someone else. I check this 'react-native-facebook-login' from https://github.com/magus/react-native-facebook-login.
The whole procedure is almost same except there is one different "header file import" in "AppDelegate.m"

  #import <FBSDKCoreKit/FBSDKCoreKit.h>
  #import <FBSDKLoginKit/FBSDKLoginKit.h> 

I add the second import of FBSDKLoginKit.h and it works.

1

There are 1 answers

0
Abhishek Ahirwar On

I am using this code in my case the Login Button work well and if you can use this code i am sure your button are work properly...

See the code here....

<LoginButton   
          publishPermissions={["email"]}
          onLoginFinished={
            (error, result) => {
              if (error) {
                alert("Login failed with error: " + error.message);
              } else if (result.isCancelled) {
                alert("Login was cancelled");
              } else {
                alert("Login was successful with permissions: " + result.grantedPermissions)
              }
            }
          }
          onLogoutFinished={() => alert("User logged out")}

          />

And the App.js file are here

import React, { Component } from 'react';
import { View,StyleSheet } from 'react-native';
import { LoginButton, AccessToken, LoginManager, } from 'react-native-fbsdk';
import { ShareApi } from 'react-native-fbsdk';
export default class App extends Component {
  componentDidNotMount() {
    LoginManager.logInWithPermissions(['public_profile']).then(
      function(result) {
        if (result.isCancelled) {
          alert('Login cancelled');
        } else {
          alert(
            'Login success with permissions: ' +
              result.grantedPermissions.toString()
          );
        }
      },
      function(error) {
        alert('Login fail with error: ' + error);
      }
    );
  }
  render() {
    return (
      <View style={styles.container}>
         <LoginButton   
          publishPermissions={["email"]}
          onLoginFinished={
            (error, result) => {
              if (error) {
                alert("Login failed with error: " + error.message);
              } else if (result.isCancelled) {
                alert("Login was cancelled");
              } else {
                alert("Login was successful with permissions: " + result.grantedPermissions)
              }
            }
          }
          onLogoutFinished={() => alert("User logged out")}

          />
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});