首页 > 解决方案 > 在 Cordova 插件中将变量传递给 AppDelegate

问题描述

有什么方法可以让我在 AppDelegate 中获取偏好值?或者至少将变量从 CDVPlugin 子类传递给 AppDelegate?

我正在为 iOS 编写一个 Cordova 插件,其中有这 2 个 Objective-C 文件,第一个用于添加额外的委托事件,第二个是 CDVPlugin 子类。

我必须访问两个文件中的首选项值才能获取应用程序密钥。

// AppDelegate

@implementation AppDelegate (AppDelegate)

- (void) application:(UIApplication*)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    // I need access the app key here
}

- (void) application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    // ...
}

- (void) application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    // ...
}

- (void) application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    // I need access the app key here
}

@end
// CDVPlugin

@implementation MyPlugin

- (void) pluginInitialize
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishLaunchingNotification:) name:UIApplicationDidFinishLaunchingNotification object:nil];
}

- (void) didFinishLaunchingNotification:(NSNotification*)notification
{
    // I need access the app key here
}

- (void) start:(CDVInvokedUrlCommand*)command
{
    // ...
}

@end

因为 CDVPlugin 子类很简单,我可以访问保存值的设置字典:

[self.commandDelegate.settings objectForKey:[@"appkey" lowercaseString]];

但是,在 AppDelegate 中,我无权访问commandDelegate或类似的东西。

所以,我的问题是:

有什么方法可以让我在 AppDelegate 中获取偏好值?或者至少将变量从 CDVPlugin 子类传递给 AppDelegate?

标签: iosobjective-ccordovacordova-pluginsappdelegate

解决方案


推荐阅读