将自定义参数传递给 Phonegap 插件
Passing custom parameters to Phonegap plugin
我编写了一个 Phonegap 插件,以便将我的 Phonegap 应用程序与本机 SDK 库集成。为了将一些设备注册信息传递给本机库,我使用 Objective-C 类别在 didFinishLaunchingWithOptions 之后插入我自己的处理程序,与 Phonegap Push Plugin 的方式几乎相同。因此,我有如下内容:
@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
apiKey 特定于我的应用程序,现在它在 AppDelegate+MyLib.m 中定义,如上面的代码所示。我的问题是,有没有办法让我的 Phonegap 应用程序在导入插件时自定义它?它可以在我的应用程序的 config.xml 中定义(或者在任何其他地方,如果有区别的话)并以某种方式作为参数传递给插件的 AppDelegate 吗?
首先,您不需要使用 swizzling,只需阅读如何从 CDVPlugin 子类获取 UIApplicationDidFinishLaunchingNotification
现在您可以在 config.xml
上使用首选项标签
<preference name="apiKey" value="yourApiKeyHere" />
然后,您在代码中(在 CDVPlugin 子类上)得到 "apiKey" 这样做:
NSString* apiKey = [self.commandDelegate.settings objectForKey:[@"apiKey" lowercaseString]];
我编写了一个 Phonegap 插件,以便将我的 Phonegap 应用程序与本机 SDK 库集成。为了将一些设备注册信息传递给本机库,我使用 Objective-C 类别在 didFinishLaunchingWithOptions 之后插入我自己的处理程序,与 Phonegap Push Plugin 的方式几乎相同。因此,我有如下内容:
@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
apiKey 特定于我的应用程序,现在它在 AppDelegate+MyLib.m 中定义,如上面的代码所示。我的问题是,有没有办法让我的 Phonegap 应用程序在导入插件时自定义它?它可以在我的应用程序的 config.xml 中定义(或者在任何其他地方,如果有区别的话)并以某种方式作为参数传递给插件的 AppDelegate 吗?
首先,您不需要使用 swizzling,只需阅读如何从 CDVPlugin 子类获取 UIApplicationDidFinishLaunchingNotification
现在您可以在 config.xml
上使用首选项标签 <preference name="apiKey" value="yourApiKeyHere" />
然后,您在代码中(在 CDVPlugin 子类上)得到 "apiKey" 这样做:
NSString* apiKey = [self.commandDelegate.settings objectForKey:[@"apiKey" lowercaseString]];