带Appdelegate框架的Cordova插件

时间:2020-10-15 11:45:02

标签: ios cordova cordova-plugins appdelegate ios-frameworks

我创建了一个带有本地sdk的框架,我工作的公司拥有该框架用于推送通知。我在此框架内有一个Appdelegate,因为有一些文件使用它。我已经将此框架添加为Cordova测试应用程序的插件,但是正如我所看到的,我只能使用现在存在的两个AppDelegates中的一个(框架中的一个,而Cordova应用程序中的一个)。我试图仅使用框架的AppDelegate,但为此我必须以某种方式使用CDVAppDelegate。问题是初始化中的应用程序未运行正确的didFinishLaunchingWithOptions函数,并且我看不到cordova应用程序的默认首页。我在插件中有此文件:

AppDelegateExtension.m

#import "xxxSDK/AppDelegate.h"
#import "xxxSDK/xxx.h"
#import "MainViewController.h"
#import "MyPlugin.h"

@implementation AppDelegate (MyPlugin)
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    #if (DEBUG == 1)
      [xxx launchWithAppUUID:@"1256fbeb638b4426a98e4bacbc5f56f5" launchOptions:launchOptions];
    #else
      [xxx launchWithAppUUID:@"1256fbeb638b4426a98e4bacbc5f56f5" launchOptions:launchOptions];
    #endif

    [[xxx sharedService].pushManager registerForRemoteNotifications];
    [[xxx sharedService].pushManager resetBadge];
    MyPlugin.myPlugin.viewController = [[MainViewController alloc] init];
    
    [MyPlugin.myPlugin application:application didFinishLaunchingWithOptions:launchOptions];
    return YES;
}

MyPlugin.h

#import <Cordova/CDVAppDelegate.h>

@interface MyPlugin : CDVAppDelegate
    // Public static method
    + (CDVAppDelegate*) myPlugin;
@end

MyPlugin.m:

#import <Foundation/Foundation.h>
#import "MyPlugin.h"
#import "MainViewController.h"

@implementation MyPlugin

// Private static reference
static CDVAppDelegate* myPlugin;

// Public static method
+ (CDVAppDelegate*) myPlugin {
    return myPlugin;
}
@end

在框架内,AppDelegate.h是这样的:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@end

我也尝试过将MyPlugin声明为CDVPlugin,但是问题没有得到解决。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

由于AppDelegateExtension.mAppDelegate类的一个类别,因此它扩展了原始类而不是实现子类,因此您在类别中实现的任何方法都已经存在于主类中(即{{ 3}}由Cordova自动生成)。

因此,在这种情况下,您在didFinishLaunchingWithOptions中的AppDelegateExtension.m将替换AppDelegate.m

TL; DR:您需要didFinishLaunchingWithOptions in AppDelegate.m以避免破坏原始方法:

@implementation AppDelegate (MyPlugin)
+ (void)load {
    Method original = class_getInstanceMethod(self, @selector(application:didFinishLaunchingWithOptions:));
    Method swizzled = class_getInstanceMethod(self, @selector(application:swizzledDidFinishLaunchingWithOptions:));
    method_exchangeImplementations(original, swizzled);
}

- (BOOL)application:(UIApplication*)application swizzledDidFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
{
    #if (DEBUG == 1)
      [xxx launchWithAppUUID:@"1256fbeb638b4426a98e4bacbc5f56f5" launchOptions:launchOptions];
    #else
      [xxx launchWithAppUUID:@"1256fbeb638b4426a98e4bacbc5f56f5" launchOptions:launchOptions];
    #endif

    [[xxx sharedService].pushManager registerForRemoteNotifications];
    [[xxx sharedService].pushManager resetBadge];
    MyPlugin.myPlugin.viewController = [[MainViewController alloc] init];
    
    [MyPlugin.myPlugin application:application didFinishLaunchingWithOptions:launchOptions];
    return YES;
}

您可以在swizzle the method中看到一个示例。

答案 1 :(得分:0)

好的,我这样做了:

gulp.series

CDVAppdelegate的didFinishLaunchingWithOptions正在运行,但是在设备index.html视图中看不到。我的SDK运行正常,但cordova应用程序根本没有运行。 我认为事情是AppDelegate:UIResponder无法支持我想做的事情,而且我开始相信我想做的事情无法实现。