2

我正在使用 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);

非常感谢任何帮助。谢谢。

更新:只是解决我的问题,希望能激励别人。我从https://github.com/magus/react-native-facebook-login检查了这个“react-native-facebook-login” 。
整个过程几乎相同,只是“AppDelegate.m”中有一个不同的“头文件导入”

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

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

4

1 回答 1

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',
  },
});
于 2019-07-08T14:49:12.803 回答