Ido Ran
Ido Ran

Reputation: 11414

Fire Event from Cordova Plugin from native implementation

I'm implementing Cordova native plugin and I want to expose events from the native implementation to the JS.
I've saw that apache/cordova-plugin-geolocation implement watchPosition by repeatedly calling the success callback until clearWatch is called.

I also found there is a method cordova.fireDocumentEvent but didn't found good documentation about it.

What is the pros and cons for each of the methods?

Upvotes: 1

Views: 999

Answers (1)

Ido Ran
Ido Ran

Reputation: 11414

I've found I'm able to call the success multiple times.
I've modified this "hello world plugin" for Cordova to call back two times (which of course can be extend):

#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

Upvotes: 1

Related Questions