I have written a Phonegap plugin in order to integrate my Phonegap application with a native SDK library. In order to pass some device registration information to the native library I have used an Objective-C category to plug my own handler after didFinishLaunchingWithOptions, pretty much the same way the Phonegap Push Plugin does. I therefore have something like the following:
@implementation AppDelegate (MyLib)
// Using method swizzling to plug our own init method which allows us to
// add our own listener for the didFinishLaunching event without overwriting
// the one already defined by Phonegap.
+ (void)load
{
Method original, swizzled;
original = class_getInstanceMethod(self, @selector(init));
swizzled = class_getInstanceMethod(self, @selector(swizzled_init));
method_exchangeImplementations(original, swizzled);
}
- (AppDelegate *)swizzled_init
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(launchHandler:)
name:@"UIApplicationDidFinishLaunchingNotification" object:nil];
// This actually calls the original init method over in AppDelegate.
return [self swizzled_init];
}
// This code will be called immediately after application:didFinishLaunchingWithOptions:.
- (void)launchHandler:(NSNotification *)notification
{
NSString* apiKey = @"xxxxx";
// Do some initialisation and register the device using the apiKey
}
@end
The apiKey is specific to my application and right now it is defined in AppDelegate+MyLib.m as shown in the code above. My question is, is there a way to allow my Phonegap app to customise this when importing the plugin? Could it be defined in my app's config.xml (or in any other place if that makes a difference) and somehow passed to the plugin's AppDelegate as a parameter?
Firts of all, you don't need to use swizzling, just read how to get the UIApplicationDidFinishLaunchingNotification from a CDVPlugin subclass
Customizing iOS's application:didFinishLaunchingWithOptions: method in a Cordova/Ionic project
Now you can use the preference tag on the config.xml
<preference name="apiKey" value="yourApiKeyHere" />
Then, you get the "apiKey" on your code (on a CDVPlugin subclass) doing this:
NSString* apiKey = [self.commandDelegate.settings objectForKey:[@"apiKey" lowercaseString]];