ios - 将自定义参数传递给 Phonegap 插件
<p><p>我编写了一个 Phonegap 插件,以便将我的 Phonegap 应用程序与原生 SDK 库集成。为了将一些设备注册信息传递给本地库,我使用了一个 Objective-C 类别在 didFinishLaunchingWithOptions 之后插入我自己的处理程序,几乎与 <a href="https://github.com/phonegap-build/PushPlugin" rel="noreferrer noopener nofollow">Phonegap Push Plugin</a> 相同。做。因此,我有以下内容:</p>
<pre><code>@implementation AppDelegate (MyLib)
// Using method swizzling to plug our own init methodwhich 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
{
[ addObserver:self selector:@selector(launchHandler:)
name:@"UIApplicationDidFinishLaunchingNotification" object:nil];
// This actually calls the original init method over in AppDelegate.
return ;
}
// 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
</code></pre>
<p>apiKey 是特定于我的应用程序的,现在它在 AppDelegate+MyLib.m 中定义,如上面的代码所示。我的问题是,有没有办法让我的 Phonegap 应用程序在导入插件时自定义它?是否可以在我的应用程序的 config.xml 中定义(或在任何其他地方,如果有影响的话)并以某种方式作为参数传递给插件的 AppDelegate?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>首先,您不需要使用 swizzling,只需阅读如何从 CDVPlugin 子类获取 UIApplicationDidFinishLaunchingNotification</p>
<p> <a href="https://stackoverflow.com/questions/31000387/customizing-ioss-applicationdidfinishlaunchingwithoptions-method-in-a-cordova/31070821" rel="noreferrer noopener nofollow">Customizing iOS's application:didFinishLaunchingWithOptions: method in a Cordova/Ionic project</a> </p>
<p>现在您可以在 config.xml 中使用偏好标签</p>
<pre><code> <preference name="apiKey" value="yourApiKeyHere" />
</code></pre>
<p>然后,您会在代码中(在 CDVPlugin 子类上)获得“apiKey”:</p>
<pre><code>NSString* apiKey = ];
</code></pre></p>
<p style="font-size: 20px;">关于ios - 将自定义参数传递给 Phonegap 插件,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33142900/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33142900/
</a>
</p>
页:
[1]