DEV Community

Aneeqa Khan
Aneeqa Khan

Posted on

How to add FB SDK in react-native project

Hi fellow coders!
Recently we needed to show our react native app ads on different platforms like Facebook or Instagram so for that you must have FB SDK installed and configured in your app.

React Native FBSDK (Not Supported)

I came to know that Facebook dropped the support from the official wrapper library and created Facebook SDKs for Android and iOS instead. But if you are only a JS dev you won't understand the swift code mentioned in their iOS SDK and you'll get confused like me.

React Native FBSDK Next

Also, they gave us the way towards the alternative library react-native-fbsdk-next. So today I want to share how I installed and configured FB SDKs through this library.

React Native FBSDK Next

This project aims to keep continuity of the React Native FBSDK from Facebook. As Facebook dropped support from it. As a community for this is our effort in order to keep upgrading and improving support for this module.

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.


Installation

React Native Compatibility

To use this library you need to ensure youโ€ฆ

Installation

npm command

npm install --save react-native-fbsdk-next
Enter fullscreen mode Exit fullscreen mode

or yarn command

yarn add react-native-fbsdk-next
Enter fullscreen mode Exit fullscreen mode

Linking

On the latest version of react-native it will link automatically for android.
for iOS, do this

cd ios/ && pod install
Enter fullscreen mode Exit fullscreen mode

Configuration

First lets go through the android steps.

- Android

1) Open the build.gradle (Module: app) file and add this line in the dependencies section

implementation 'com.facebook.android:facebook-android-sdk:latest.release'
Enter fullscreen mode Exit fullscreen mode

2) Now open the /app/res/values/strings.xml file and add these 2 lines

<string name="facebook_app_id">1234</string>
<string name="facebook_client_token">56789</string>
Enter fullscreen mode Exit fullscreen mode

Replace 1234 with your Facebook app id and 56789 with client token value from the Facebook developer dashboard.
3) After that open the /app/manifests/AndroidManifest.xml file and add meta-data to the application element.

<application android:label="@string/app_name" ...>
    ...
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
    <meta-data android:name="com.facebook.sdk.ClientToken" android:value="@string/facebook_client_token"/>
    ...
</application>
Enter fullscreen mode Exit fullscreen mode

4) And don't forget to add the uses-permission element in the AndroidManifest.xml file after the application element.

<uses-permission android:name="android.permission.INTERNET"/>
Enter fullscreen mode Exit fullscreen mode

5) Save all files and build your project and you are done. ๐ŸŽ‰
Let's move on to the iOS section now.

- iOS

1) Open your info.plist file and add these lines in <dict>...</dict> tag

<key>CFBundleURLTypes</key>
<array>
  <dict>
  <key>CFBundleURLSchemes</key>
  <array>
    <string>fbAPP-ID</string>
  </array>
  </dict>
</array>
<key>FacebookAppID</key>
<string>APP-ID</string>
<key>FacebookClientToken</key>
<string>CLIENT-TOKEN</string>
<key>FacebookDisplayName</key>
<string>APP-NAME</string>
Enter fullscreen mode Exit fullscreen mode

Now here replace fbAPP-ID and APP-ID with your Facebook id available in the Facebook developer app dashboard. Put your client token in place of CLIENT-TOKEN available in Advanced Settings and the last replace APP-NAME with your app name.
2) Now open your AppDelegate.m file and paste this code there.

#import <FBSDKCoreKit/FBSDKCoreKit.h> // <- Add This Import
#import <React/RCTLinkingManager.h> // <- Add This Import

- (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  if ([[FBSDKApplicationDelegate sharedInstance] application:app openURL:url options:options]) {
    return YES;
  }

  if ([RCTLinkingManager application:app openURL:url options:options]) {
    return YES;
  }

  return NO;
}
Enter fullscreen mode Exit fullscreen mode

3) Initialise FB SDK in the same file. Go to the didFinishLaunchingWithOptions function and paste this line before return YES; statement

[FBSDKApplicationDelegate.sharedInstance initializeSDK];
Enter fullscreen mode Exit fullscreen mode

4) Now in the last as I wanted our app for Advertising and for iOS 14 devices facebook asked to enable the Advertising tracking flag. So to solve this issue I added this line in my App.js file.

import {Settings} from 'react-native-fbsdk-next';
...
  useEffect(() => {
    Settings.setAdvertiserTrackingEnabled(true);
  }, []);
Enter fullscreen mode Exit fullscreen mode

5) After that build your app and you are done. ๐ŸŽ‰

Result

You'll start seeing events in the Events Manager of your Facebook app.

facebook events

And also you'll see the activity of the last installs on your Facebook developer dashboard

facebook dashboard

Thank you for reading! Feel free to connect on Twitter

Discussion (2)

Collapse
frankfuu profile image
Frank Fu

I also want to add that if you got the error. "use of undeclared identifier 'FBSDKApplicationDelegate" you might want to visit this github issue

github.com/facebookarchive/react-n...

Collapse
uziel302 profile image
Asaf M • Edited on

I followed the instructions and was getting error:
No visible @interface for 'FBSDKApplicationDelegate' declares the selector 'initializeSDK'
I had react-native-fbsdk-next previously installed, so I had to delete its reference from package.json and package-lock.json and rerun:
npm install --save react-native-fbsdk-next
pod install
to get the latest versions, where initializeSDK is supported

Another caveat, I was sending custom events, so I couldn't see them on default events manager view, had to switch to "all events" to see them.