Search code examples
ioscordovanativecordova-plugins

How to handle Javascript callbacks in cordova iOS plugin


So I have a cordova iOS plugin that spawns a uiView

[self.pluginviewContoller presentViewController:self.myView animated:YES completion:nil];

Now in the view controller I want to do callbacks to my javascript code on specific events. I have tried instantiating the class that inherits CDVPlugin but it does not work.

How do I go about doing this in the right way?


Solution

  • Maybe something like this on events:

    NSDictionary *payload = [NSDictionary dictionaryWithObjectsAndKeys:
        messageParameter1, @"message1", 
        messageParameter2, @"message2", 
        nil];
    
    CDVPluginResult* pluginResult = nil;
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:payload];
    [pluginResult setKeepCallbackAsBool:YES];
    
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    

    [pluginResult setKeepCallbackAsBool:YES]; is the key, as if you do not add that, the callback is finalized and will accept no further events.