Unobtrusively extending AppController delegates. Possible?

Hi,

I’m developing a Unity Plug-in for iOS. To do so, I’ve created my C/Objective-C library, and a binding (so I can call it in my C# scripts). However, I’d like my plug-in to extend application delegates (such as applicationDidBecomeActive) and code them in my plug-in manager class, so that I don’t touch the AppController.mm file at all. I need to do this because I want everything as automatized as possible (I’m using a custom-made post-build system to automatically update my XCode project). I’ve thought about Objective-C’s categories, but I don’t want to override the complete class (I don’t want to lose the AppController original implementation) but extend it. Is there a programmatically way to properly extend the AppController delegates in my class, or any way to get my class noticed whenever an AppController delegate is called?

Thanks!

If you just want to be able to react to app state changes, you can do some of this by creating your own class, instantiating it, and telling it to listen for the notification event that corresponds to the app delegate method. For instance, you can create something like the following file in Assets/Plugins/iOS:

// BackgroundWatcher.m
#import <UIKit/UIKit.h>

@interface BackgroundWatcher : NSObject
@end

__strong BackgroundWatcher *sharedInstance;

@implementation BackgroundWatcher

+ (void)load {
    sharedInstance = [[self alloc] init];
}

- (id)init {
    if (!(self = [super init])) return nil;
    
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserverForName:UIApplicationDidFinishLaunchingNotification
                    object:nil
                     queue:[NSOperationQueue mainQueue]
                usingBlock:^(NSNotification *note) {
                    NSLog(@"App finished launching! Hooray!");
                }];
    return self;
}

@end

The code in the “load” method (which you can think of as similar to MonoBehavior.Awake, but in this case is a class (static) method that is called when the class is loaded at runtime) creates a single instance of this class. The code in “init” sets up that instance to listen for the UIApplicationDidFinishLaunchingNotification, which is an event that is posted just after the correspond app delegate method is called. The code inside the block (where the NSLog call is shown here) is what will be executed when the notification occurs.

So this works in the general case. So far I’ve found one case where I had to actually insert some code into the app delegate instead, though. I wanted to send a message to unity in response to the app going to background. By the time that notification is sent, the app delegate has already paused Unity so it’s too late :frowning:

I wanted the same, just made a writeup on this:

Override app delegate in Unity for iOS and OSX (1/4) Plugin workflow.

Basically two approach, one is doing with Notification Center, the other uses Method swizzling. Also there is an alternative for OSX.