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

Top comments (11)

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
 
mohdumer profile image
Muhammad Umer

You only just need to declare
this import..

import

above the...

ifdef FB_SONARKIT_ENABLED in AppDelegate.m file

Now you are able to archive easily..🀩🀩🀩

Collapse
 
qasimgit profile image
MUHAMMAD QASIM QADRI • Edited

Hey @aneeqakhan, thanks for writing this beautiful article,
just wanted to ask there are some steps which include swift code, I am not seeing these steps in your article, can you please add those steps too, very confused in how to add these swift code lines in react native, you can check those steps in Getting started Guide.
Thanks

Image description

Collapse
 
aneeqakhan profile image
Aneeqa Khan

Yes, just like mentioned in your screenshot that step 3 and 4 have swift code in official docs of FB SDK, so thats why fbsdk-next guide provided that code in Objective-C under the Note text.

Collapse
 
uziel302 profile image
Asaf M • Edited

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.

Collapse
 
bishalsaha profile image
Bishal Saha

Replace

#import <FBSDKCoreKit/FBSDKCoreKit.h>

to

#import <FBSDKCoreKit/FBSDKCoreKit-Swift.h>

Collapse
 
dhannyphantom profile image
Olojo Daniel Oluwaseun

Does this package work with Expo?

Collapse
 
aneeqakhan profile image
Aneeqa Khan

I guess no, there is no installation guide provided for expo

Collapse
 
shuzo-nomad profile image
Shuzo Otani
Collapse
 
shuzo-nomad profile image
Shuzo Otani
Collapse
 
yprince19 profile image
yprince19 • Edited

Hi @aneeqakhan thanks for this awesome blog.
I want to add multiple fb-pixel accounts for tracking. How to do this?