来自本机实现的 Cordova 插件的 Fire Event

Fire Event from Cordova Plugin from native implementation

我正在实现 Cordova 本机插件,我想将本机实现中的事件公开给 JS。
我已经看到 apache/cordova-plugin-geolocation 通过重复调用成功回调直到调用 clearWatch 来实现 watchPosition

我还发现有一个方法 cordova.fireDocumentEvent 但没有找到关于它的好文档。

每种方法的优缺点是什么?

我发现我可以多次调用成功。
我修改了这个 "hello world plugin" for Cordova 以回调两次(当然可以延长):

#import "HWPHello.h"

@implementation HWPHello

- (void)greet:(CDVInvokedUrlCommand*)command
{
    NSString* name = [[command arguments] objectAtIndex:0];
    NSString* msg = [NSString stringWithFormat: @"Welcome Mr. %@", name];

    CDVPluginResult* result = [CDVPluginResult
                               resultWithStatus:CDVCommandStatus_OK
                               messageAsString:msg];

    // Set the KeepCallback before sending the result will keep the callback id for further callbacks
    [result setKeepCallbackAsBool:YES];
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];

    msg = [NSString stringWithFormat: @"Hi Mr. %@", name];
    result = [CDVPluginResult
                                   resultWithStatus:CDVCommandStatus_OK
                                   messageAsString:msg];
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

@end