Add Facebook Analytics in Buildbox iOS Game

Let me get to the point and see how we can add Facebook analytics in Buildbox exported ios game. 

  • Create new Facebook app from facebook developer console.
  • Add iOS platform and add package id
  • Go to root folder of the exported project
  • Open terminal and run pod init which will create a new file named ‘Podfile

Setup Pod

Open Podfile and add Facebook core pod dependency. 

platform :ios, '9.0'
source 'https://github.com/CocoaPods/Specs.git'

#use_frameworks!
#
target 'BBPlayer' do
 # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for BBPlayer
  pod 'Bolts'
  pod 'FBSDKCoreKit'

end

Save and close file. Open terminal and run pod install command.

You will get an output similar to this

pod install
Analyzing dependencies
Downloading dependencies
Using Bolts (1.9.0)
Using FBSDKCoreKit (4.39.0)
Using nanopb (0.3.901)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There are 3 dependencies from the Podfile and 9 total pods installed.

[!] The `BBPlayer [Debug]` target overrides the `HEADER_SEARCH_PATHS` build setting defined in `Pods/Target Support Files/Pods-BBPlayer/Pods-BBPlayer.debug.xcconfig'. This can lead to problems with the CocoaPods installation
- Use the `$(inherited)` flag, or
- Remove the build settings from the target.

[!] The `BBPlayer [Release]` target overrides the `GCC_PREPROCESSOR_DEFINITIONS` build setting defined in `Pods/Target Support Files/Pods-BBPlayer/Pods-BBPlayer.release.xcconfig'. This can lead to problems with the CocoaPods installation
- Use the `$(inherited)` flag, or
- Remove the build settings from the target.

[!] The `BBPlayer [Release]` target overrides the `HEADER_SEARCH_PATHS` build setting defined in `Pods/Target Support Files/Pods-BBPlayer/Pods-BBPlayer.release.xcconfig'. This can lead to problems with the CocoaPods installation
- Use the `$(inherited)` flag, or
- Remove the build settings from the target.

Open BBPlayer.xcworkspace workspace. 

Facebook properties in info.plist

Now we have to add Facebook app information in our info.plist file

  • Right click on info.plist and open as Source Code
  • Add and replace property value as per your Facebook app id
<key>CFBundleURLTypes</key>
<array>
  <dict>
  <key>CFBundleURLSchemes</key>
  <array>
    <string>fb425198248223614</string>
  </array>
  </dict>
</array>
<key>FacebookAppID</key>
<string>425198248223614</string>
<key>FacebookDisplayName</key>
<string>Buildbox Example</string>

Build Settings

  • Open build settings and add ${inherited} flag in other linker flags

Update AppDelegate.mm

Open AppDelegate.mm file and add Facebook header 

#import <FBSDKCoreKit/FBSDKCoreKit.h>

Update didFinishLaunchingWithOptions function

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[FBSDKApplicationDelegate sharedInstance] application:application
                             didFinishLaunchingWithOptions:launchOptions];
    
    return YES;
}

Add openURL function

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    
    BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
                                                                  openURL:url
                                                        sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                                               annotation:options[UIApplicationOpenURLOptionsAnnotationKey]
                    ];
    // Add any custom logic here.
    return handled;
}

Build and Run

  • You are ready to test your ios game with Facebook analytics

Add Custom Facebook Analytics Events

Add header in AppDelegate.mm

#include "screens/PTPScoreController.h"

Now you can add these two functions in your delegate.

- (void)screenOnEnter:(const char*) name{
}

- (void)screenOnExit:(const char*) name{
}

screenOnEnter is called everytime when a new screen is opened. 

screenOnExit is called everytime when the screen is closed and navigated to a new screen. 

If you are looking to implement Facebook or Firebase analytics in Buildbox Android then you can check this article.

Leave a Reply