• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 将自定义参数传递给 Phonegap 插件

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:36:53 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我编写了一个 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 selectorselector(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)launchHandlerNSNotification *)notification
{
    NSString* apiKey = @"xxxxx";
    // Do some initialisation and register the device using the apiKey
}

@end

apiKey 是特定于我的应用程序的,现在它在 AppDelegate+MyLib.m 中定义,如上面的代码所示。我的问题是,有没有办法让我的 Phonegap 应用程序在导入插件时自定义它?是否可以在我的应用程序的 config.xml 中定义(或在任何其他地方,如果有影响的话)并以某种方式作为参数传递给插件的 AppDelegate?



Best Answer-推荐答案


首先,您不需要使用 swizzling,只需阅读如何从 CDVPlugin 子类获取 UIApplicationDidFinishLaunchingNotification

Customizing iOS's application:didFinishLaunchingWithOptions: method in a Cordova/Ionic project

现在您可以在 config.xml 中使用偏好标签

 <preference name="apiKey" value="yourApiKeyHere" />

然后,您会在代码中(在 CDVPlugin 子类上)获得“apiKey”:

NSString* apiKey = [self.commandDelegate.settings objectForKey:[@"apiKey" lowercaseString]];

关于ios - 将自定义参数传递给 Phonegap 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33142900/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap