2017-04-15 74 views
0

我設置了我的xcode項目,以便成功使用facebook庫。我有下面的代碼在我index.ios.js文件:如何將Facebook登錄界面添加到我的React Native應用程序中?

/** 
* Sample React Native App 
* https://github.com/facebook/react-native 
* @flow 
*/ 

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


export default class FacebookReactNativeTest extends Component { 
    render() { 
    return (
     <View style={styles.container}> 
     <Text style={styles.welcome}> 
      Welcome to React Native! 
     </Text> 
     <Text style={styles.instructions}> 
      To get started, edit index.ios.js 
     </Text> 
     <Text style={styles.instructions}> 
      Press Cmd+R to reload,{'\n'} 
      Cmd+D or shake for dev menu 
     </Text> 
     </View> 
    ); 
    } 
} 

const styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    justifyContent: 'center', 
    alignItems: 'center', 
    backgroundColor: '#F5FCFF', 
    }, 
    welcome: { 
    fontSize: 20, 
    textAlign: 'center', 
    margin: 10, 
    }, 
    instructions: { 
    textAlign: 'center', 
    color: '#333333', 
    marginBottom: 5, 
    }, 
}); 

AppRegistry.registerComponent('FacebookReactNativeTest',() => FacebookReactNativeTest); 

可悲的是,Facebook登錄的東西是不會出現在這裏查看,但在FacebookReactNativeTest的東西。看來我沒有添加創建到視圖的Login var,因爲我是React Native的新手,我不確定如何手動添加它。我怎樣才能做到這一點?

回答

0

想通了這是怎麼了應該做的事:

/** 
* Sample React Native App 
* https://github.com/facebook/react-native 
* @flow 
*/ 

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



export default class FacebookReactNativeTest extends Component { 
    render() { 
    return (
     <View style={styles.container}> 
     <Text style={styles.welcome}> 
      Welcome to React Native! 
     </Text> 
     <Text style={styles.instructions}> 
      To get started, edit index.ios.js 
     </Text> 
     <Text style={styles.instructions}> 
      Press Cmd+R to reload,{'\n'} 
      Cmd+D or shake for dev menu 
     </Text> 
     <Text>Welcome to the Facebook SDK for React Native!</Text> 
     <Login /> 
     </View> 
    ); 
    } 
} 

const styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    justifyContent: 'center', 
    alignItems: 'center', 
    backgroundColor: '#F5FCFF', 
    }, 
    welcome: { 
    fontSize: 20, 
    textAlign: 'center', 
    margin: 10, 
    }, 
    instructions: { 
    textAlign: 'center', 
    color: '#333333', 
    marginBottom: 5, 
    }, 
}); 

AppRegistry.registerComponent('FacebookReactNativeTest',() => FacebookReactNativeTest); 
相關問題