React Native FBSDK
React Native FBSDK is a wrapper around the iOS Facebook SDK and Android Facebook SDK, allowing for Facebook integration in React Native apps. Access to native components, from login to sharing, is provided entirely through documented JavaScript modules so you don't have to call a single native function directly.
Functionality is provided through one single npm package so you can use it for both platforms without downloading any extra packages. Follow this guide to use react-native-fbsdk in your react-native app. You can also visit https://developers.facebook.com/docs/react-native for tutorials and reference documentation.
GIVE FEEDBACK
Please post questions on sdk set up to stackoverflow for quicker response. Besides it's easier for others searching for similar questions. Report bugs or issues to https://developers.facebook.com/bugs/
Installation
You need to install the sdk with npm and configure native Android/iOS project in the react native project.
1. Create React Native project
First create a React Native project:
react-native init YourApp
2. Install JavaScript packages
Install and link the react-native-fbsdk package:
react-native install react-native-fbsdkreact-native link react-native-fbsdk
3. Configure native projects
3.1 Android project
Assuming you have Android Studio installed, open the project with Android Studio.
If your react-native version is below 0.29.0
Go to MainActivity.java
under app/src/main/java/com/<project name>/
to complete setup.
Note that packages must be imported to use.
Add an instance variable of type CallbackManager
in class.
// <--- import
Register sdk package in method getPackages()
.
@Overrideprotected List<ReactPackage> { mCallbackManager = new CallbackManager.Factory().; ReactPackage packages[] = new ReactPackage[]{ new MainReactPackage(), new FBSDKPackage(mCallbackManager), }; return Arrays.<ReactPackage>;}
Override onActivityResult()
.
@Overridepublic void { super.; mCallbackManager.;}
Before you can run the project, follow the Getting Started Guide for Facebook Android SDK to set up a Facebook app. You can skip the build.gradle changes since that's taken care of by the rnpm link step above, but make sure you follow the rest of the steps such as calling FacebookSdk.sdkInitialize
and updating strings.xml
and AndroidManifest.xml
. Note that react-native project doesn't have the Application class, so you'll need to create an implementation of the Application class yourself.
If your react-native version is 0.29 or above
Go to MainApplication.java
and MainActivity.java
under app/src/main/java/com/<project name>/
to complete setup.
In MainApplication.java
,
Add an instance variable of type CallbackManager
and its getter.
...
Override onCreate()
method
@Overridepublic void { super.; FacebookSdk.; // If you want to use AppEventsLogger to log events. AppEventsLogger.;}
Register sdk package in method getPackages()
.
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) ;
In MainActivity.java
Override onActivityResult()
method
Before you can run the project, follow the Getting Started Guide for Facebook Android SDK to set up a Facebook app. You can skip the build.gradle changes since that's taken care of by the rnpm link step above, and the step of calling FacebookSdk.sdkInitialize
. But make sure you follow the rest of the steps such as updating strings.xml
and AndroidManifest.xml
.
3.2 iOS project
The react-native-fbsdk has been linked by rnpm, the next step will be downloading and linking the native Facebook SDK for iOS.
Make sure you have the latest Xcode installed. Open the .xcodeproj in Xcode found in the ios
subfolder from your project's root directory. Now, follow all the steps in the Getting Started Guide for Facebook SDK for iOS. Along with FBSDKCoreKit.framework
, don't forget to import FBSDKShareKit.framework
and FBSDKLoginKit.framework
into your Xcode project.
3.3 Troubleshooting
- I cannot run the Android project.
- Make sure you added the code snippet in step 3.1.
- Make sure you set up a Facebook app and updated the
AndroidManifest.xml
andres/values/strings.xml
with Facebook app settings.
- I get a build error stating that one of the Facebook SDK files was not found -- eg.
FBSDKLoginKit/FBSDKLoginKit.h file not found
.
- Make sure that the Facebook SDK frameworks are installed in
~/Documents/FacebookSDK
. - Make sure that
FBSDK[Core, Login, Share]Kit.framework
show up in the Link Binary with Libraries section of your build target's Build Phases. - Make sure that
~/Documents/FacebookSDK
is in the Framework Search Path of your build target's Build Settings.
- I get build errors like
Warning: Native component for "RCTFBLikeView" does not exist
:
- Make sure that
libRCTFBSDK.a
shows up in the Link Binary with Libraries section of your build target's Build Phases.
- I get this build error:
no type or protocol named UIApplicationOpenURLOptionsKey
:
- Your XCode version is too old, upgrade to XCode 8.0+.
Usage
Login
Login Button + Access Token
const FBSDK = ;const LoginButton AccessToken } = FBSDK; var Login = React;
Requesting additional permissions with Login Manager
You can also use the Login Manager with custom UI to perform Login.
const FBSDK = ;const LoginManager = FBSDK; // ... // Attempt a login using the Facebook login dialog asking for default permissions.LoginManager;
Sharing
Share dialogs
All of the dialogs included are used in a similar way, with differing content types. All content types are defined with Flow Type Annotation in js/models directory.
const FBSDK = ;const ShareDialog = FBSDK; // ... // Build up a shareable link.const shareLinkContent = contentType: 'link' contentUrl: "https://facebook.com" contentDescription: 'Wow, check out this great site!'; // ... // Share the link using the share dialog. { var tmp = this; ShareDialog;}
Share API
Your app must have the publish_actions
permission approved to share through the share API. You should prefer to use the Share Dialogs for an easier and more consistent experience.
const FBSDK = ;const ShareApi = FBSDK; // ... // Build up a shareable link.const shareLinkContent = contentType: 'link' contentUrl: "https://facebook.com" contentDescription: 'Wow, check out this great site!'; // ... // Share using the share API.ShareApi;
Analytics
App events
const FBSDK = ;const AppEventsLogger = FBSDK; // ... // Log a $15 purchase.AppEventsLogger
Graph API
Graph Requests
const FBSDK = ;const GraphRequest GraphRequestManager = FBSDK; // ... //Create response callback.
License
See the LICENSE file.
Platform Policy
Developers looking to integrate with the Facebook Platform should familiarize themselves with the Facebook Platform Policy.