Prevent Facebook iOS SDK from killing your apps by feature flag

Jackson Chung
GOGOX Technology
Published in
4 min readJul 29, 2020

In one evening when we were having the company anniversary party, one guy from the senior management team came to me and said our iOS app was crashing whenever it is launched. It was not the first time that we encounter this crashing behaviour. At that moment, what came up in my head is:

Is this the Facebook SDK AGAIN? 🤔

As usual, we checked the crash logs and ran our app in debug mode to check if we could reproduce. BINGO! What we got in the crash report were thousands of crash events related to Facebook Graph API, which we have not called in our app. 😅

A stacktrace for debugging a crash

While the engineers were checking in a technical way, the managers were checking if other apps were crashing like our app and were searching if the same has been report by others on the internet. It turned out we could find people talking about the same on Twitter and some forums.

So, what can we do? At that moment, we could just pray and wait for Facebook to fix for the iOS world. Indeed, there was nothing we could do to save our app and our users. If the app is critical to a company’s business, it is a disaster because money stops until the app works properly.

🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

After a few hours, everything resumed normal. Thanks God. Everyone got relieved. However, there are follow-up questions to all of us:

Can we take over the app crash?
Can we take over the gate?

During the crash period, we tried to remove all Facebook-related code in our app to see if the crash can be stopped. It turned out the crash was still happening after removing all the related code. Our app can be crashed by just importing the Facebook SDK in the project. At that moment, we thought we were not able to do anything unless we remove our Facebook login feature. Looks like Facebook has done something tricky.

🎭 🎭 🎭 🎭 🎭 🎭 🎭 🎭 🎭 🎭 🎭 🎭 🎭 🎭 🎭 🎭 🎭 🎭 🎭 🎭 🎭 🎭 🎭 🎭🎭

As Facebook SDK is a open-source project, we are able to investigate what is going on inside. let’s dig deep! 🔥

After checking for a while, we found their trick! 🧐

https://github.com/facebook/facebook-ios-sdk/blob/master/FBSDKCoreKit/FBSDKCoreKit/FBSDKApplicationDelegate.m

The static load function of FBSDKApplicationDelegate has been automatically called at app launch because the SDK has been imported in our app. The auto initialization explains why Facebook SDK can make Graph API request without being called by us. 🙄

From Apple’s documentation

Invoked whenever a class or category is added to the Objective-C runtime; implement this method to perform class-specific behavior upon loading.

The load message is sent to classes and categories that are both dynamically loaded and statically linked, but only if the newly loaded class or category implements a method that can respond.

As we can see their code, what we can do is to make [FBSDKSettings isAutoInitEnabled] return false.

https://github.com/facebook/facebook-ios-sdk/blob/master/FBSDKCoreKit/FBSDKCoreKit/FBSDKApplicationDelegate.h

Based on the documentation in the SDK, we can turn off the automatic initialization by setting FacebookAutoInitEnabled to NO in Info.plist of the project.

Turn off Facebook SDK auto initialization in Info.plist

Since then, we have added feature flag support on the Facebook SDK initialization in our app as follows:

if FeatureFlag.facebookSDK {
// initialize Facebook SDK manually
ApplicationDelegate.initializeSDK(nil)
}

After having implemented our feature flag, even if the Facebook SDK crashes our app again, we can turn the flag off ourselves to stop the crash like controlling a dam. Even though we cannot change Facebook SDK, we can save our users with our hands. We no longer need to pray for a quick fix from Facebook. 😁

Reference:

--

--