Technical Insight: The Tricky Bug Within Facebook SDK for Unity
Dzmitry Bazyleu
Lead Unity Developer | Certified Unity Expert | 14+ years of experience | Game Development
Issue Description
There is a serious issue in relatively new Unity versions for all streams: 2022.3.17f1+, 2023.2.5f1+, 2023.3.0b2+. If the app has the Facebook SDK and uses deep links, it crashes when a user tries to open it via a deep link on iOS platform. Stack trace will be the following:
Why is the issue so tricky?
This is a very tricky case because the root of the issue is super hard to detect in a real project. You just update minor version of Unity and get crashes on notifications. "There is no clue in the stack trace that points to the Facebook SDK. Obviously, the search will be directed to the push notifications library or searching for a problem with deep links.
Technical Details
Facebook SDK has code that was committed 9 year ago that patch unity file - UnityAppController.mm. Here is the code (link to GitHub):
This code replaces 'return NO' in original function to 'return YES' in UnityAppController.mm:
Looks like this hack was added to Facebook SDK a long time ago and was forgotten. But Unity Technologies just refactored this method recently, and this hack started changing code in an unexpected place, which was the root cause of the crash.
How to fix
In order to fix the crash, the modified UnityAppController.mm should be reverted. Ready solution (source):
How such cases can be avoided
It was a bad idea on the Facebook SDK's side to implement such a hack. Some other methods should be used. However if such modification is inevitable, it should be protected more strict regex: it should contain the full modified function in its body to avoid modifying the code if the logic of the function is changed (refactored).
Unity Technical Lead & Principal Engineer | 14+ years in Game Development | Proven Track Record of Delivering 50M+ Downloads Titles | Enthusiastic about Enhancing User Experience & Streamlining Development Processes
10 个月Thanks Dzmitry Bazyleu ! That is madness from Facebook to apply regex to the code in Unity. Which leads I see from Unity Facebook sdk: no backward test compatibility with different Unity versions poor design approach for the sdk No enough focus to support it properly (370 opened issues in their GitHub)