react-native-fbsdk loginbutton运行不正常

时间:2017-01-03 02:11:02

标签: react-native react-native-fbsdk

我正在使用react-native-fbsdk 0.4.0并按照https://github.com/facebook/react-native-fbsdk中的步骤操作。我在添加原生FBSDK后尝试了原生的Object-C测试应用程序,并且FB登录按钮可以正常工作。

但是,我遇到了与此帖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);

真的很感激任何帮助。感谢。

更新:只是解决我的问题,希望可以激励别人。我查看了这个&#39; react-native-facebook-login&#39;来自https://github.com/magus/react-native-facebook-login
除了有一个不同的&#34;头文件导入&#34;之外,整个过程几乎相同。在&#34; AppDelegate.m&#34;

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

我添加了FBSDKLoginKit.h的第二个导入,它可以工作。

1 个答案:

答案 0 :(得分:0)

在我的情况下,我正在使用此代码登录按钮可以正常工作,如果您可以使用此代码,请确保您的按钮可以正常工作...

在此处查看代码。...

<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")}

          />

和App.js文件在此处

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