Recently, we needed to show ads from various platforms, like Facebook and Instagram for our React Native app.
To achieve this, having the Facebook SDK installed and properly configured is essential. Here’s a guide on setting up the SDK in your React Native for both Andriod And IOS:
npm install react-native-fbsdk-next
This package is a community-maintained alternative to the original react-native-fbsdk and is recommended for compatibility with newer versions of React Native. I want the readers to read it carefully as it contains many important docs.
npx react-native link react-native-fbsdk-next
If you’re using React Native 0.60 or higher, this step is generally not needed as auto-linking should work.
And for IOS you have to install the SDK using cocopods
cd ios/ && pod install
Let's start by going through the steps for Android setup first.
dependencies {
your code here
.................
// Meta SDK
implementation 'com.facebook.android:facebook-android-sdk:latest.release'
}
resources {
your code here
.................
<string name="facebook_app_id">0000000</string>
<string name="facebook_client_token">000000</string>
}
<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>
🎉 that's it 🎉 save all files and build your project. Let's move on to IOS.
<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>
#import <AuthenticationServices/AuthenticationServices.h>
#import <SafariServices/SafariServices.h>
#import <FBSDKCoreKit/FBSDKCoreKit-Swift.h>
One thing to keep an eye on here is the location where you past the import if you are using an old version of react-native 0.67.5 and below. Make sur to past the import outside of #ifdef FB_SONARKIT_ENABLED
// Meta SDK
#import <FBSDKCoreKit/FBSDKCoreKit-Swift.h>
#import <AuthenticationServices/AuthenticationServices.h>
#import <SafariServices/SafariServices.h>
#ifdef FB_SONARKIT_ENABLED
#import <FlipperKit/FlipperClient.h>
#import <FlipperKitLayoutPlugin/FlipperKitLayoutPlugin.h>
.....
#endif
[[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
now in the same file go down and add this:
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
return [[FBSDKApplicationDelegate sharedInstance]application:app
openURL:url
options:options];
}
If you're using React Native's RCTLinkingManager or deep linking go donw to (BOOL)application:(UIApplication *)application replace it with this:
- (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;
}
Now go to app.tsx or any other entry point in your project and initiate the SDK by adding these lines:
import { Settings } from "react-native-fbsdk-next"
Settings.initializeSDK()
npm install react-native-tracking-transparency
cd ios
pod install
thene add NSUserTrackingUsageDescription to your Info.plist
<key>NSUserTrackingUsageDescription</key>
<string>We use Tracking to fix bugs and improve your experience.</string>
Last add this to your App.tsx file
import { Settings } from "react-native-fbsdk-next"
import { requestTrackingPermission } from "react-native-tracking-transparency"
useEffect(() => {
const trackingStatus = await requestTrackingPermission()
if (trackingStatus === "authorized" || trackingStatus === "unavailable") {
await Settings.setAdvertiserTrackingEnabled(true)
}
}, []);
Requires iOS 14. On Android and iOS versions below 14, this will always return 'unavailable'.
You'll start seeing events in the Events Manager of your Facebook app like the image below: