Running Unreal Engine 4 Plugin’s code in didFinishLaunchingWithOptions on iOS

Now, that’s a long title! Some time ago I faced a very specific problem when implementing a third party iOS SDK into Unreal Engine 4. The SDK required to run its initializing function inside the didFinishLaunchingWithOptions method.

Usually when handling application lifecycle events Unreal Engine 4 uses delegates from FCoreDelegates class like ApplicationWillEnterBackgroundDelegate or ApplicationWillTerminateDelegate. But didFinishLaunchingWithOptions is one of the first methods that runs after the app is launched and at this point Unreal Engine 4 is not initialized yet. Delegates and plugins will not work!

We could modify the engine’s source code and paste the SDK’s code into the ApplicationCore module but this would be a terrible solution as all third party SDKs should be handled by plugins. Here I’d like to show a solution to this problem.

The idea is to create a static observer which will be initialized before the app launch and that can react to the didFinishLaunchingWithOptions occurence. Lets create a MySDKObserver.cpp and MySDKObserver.h files inside the IOS directory of the plugin. Thanks to that it will compile only for IOS platform. For more details of how does it work – please read the Multiplatform Plugins in Unreal Engine 4 post.

Inside MySDKObserver.h file simply declare an observer class:
@interface MySDKObserver : NSObject
@end
Inside MySDKObserver.cpp file put a following code:
@implementation MySDKObserver
 
static MySDKObserver* MySDKObserverInstance = nil;
 
-(id)init
{
    self = [super init];
    return self;
}
 
+(void)load 
{
    [super load];
    if (MySDKObserverInstance == nil)
    {
        MySDKObserverInstance = [[MySDKObserver alloc] init];
        [MySDKObserverInstance registerLifeCycleListener];
    }
}
 
-(void)registerLifeCycleListener
{
    [[NSNotificationCenter defaultCenter]   addObserver :    self
                                            selector :       @selector(didFinishLaunching:)
                                            name :           UIApplicationDidFinishLaunchingNotification
                                            object :         nil];
}
 
-(void)didFinishLaunching : (NSNotification*)notification
{
    // Do the initialization thing!
}
 
@end
The goal of this code is to utilize a load method, which runs whenever a class is added to the Objective-C runtime, to create a singleton object way before any lifecycle event occurs.

Right after the object has been created it registers itself as an observer that will react to UIApplicationDidFinishLaunchingNotification notification by running a didFinishLaunching method.

Inside the didFinishLaunching method you can paste any code you need to, for example, properly initialize the third party SDK.

Have in mind that this method will run right AFTER the original didFinishLaunchingWithOptions

If you don’t want to relay on UE4’s lifecycle delegates you can add more observers for other events. The full list of available notifications is here.

That’s pretty all. Even though UE4 does not allow to fully control lifecycle events of IOS you can still utilize features of Objective-C itself. Hope it will help in future plugins implementations. Cheers!