Can method_exchangeImplementations work between objective-c and swift?

202 Views Asked by At

I'm trying to get a Cordova plugin to work with a Capacitor project.

Capacitor is in Swift and the plugin is in Obj-c.

The plugin runs this bit of code to swizzle itself in the app start flow.

+ (void)load {
    Method original = class_getInstanceMethod(self, @selector(application:didFinishLaunchingWithOptions:));
    Method swizzled = class_getInstanceMethod(self, @selector(application:swizzledDidFinishLaunchingWithOptions:));
    method_exchangeImplementations(original, swizzled);
}

original (application:didFinishLaunchingWithOptions) is in the Swift AppDelegate and swizzled (application:swizzledDidFinishLaunchingWithOptions) is in the plugin's obj-c code.

This doesn't work.

When the app starts, this bit of code runs, but the result of class_getInstanceMethod for original is always nil.

The swift delegate has

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

And the obj-c plugin has

- (BOOL)application:(UIApplication *)application swizzledDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   
    // ...

}

I've tried adding @objc and dynamic to the swift func but that didn't help.

Is there a way to make this work?

0

There are 0 best solutions below