Keep callback context in PhoneGap plugin?

后端 未结 3 879
深忆病人
深忆病人 2021-01-13 10:05

I need to implement some functionality that triggers an action on an interval and emits the results back to javascript.

To simplify things I will use the echo examp

相关标签:
3条回答
  • 2021-01-13 10:46

    Just if it helps somebody, in Android I am not using PluginResult and I am still able to keep a reference to the CallbackContext and call it anytime. I am not sure if this is the right way, but I can confirm that it worked for me.

    0 讨论(0)
  • 2021-01-13 10:47

    You'll want to have something like:

    NSString *myCallbackId;
    

    as an instance-level variable (outside of any method, so it retains its value). Set it when you first come in to the plugin code:

    myCallbackId = command.callbackId;
    

    Then, right after you instantiate a PluginResult, but before using it, do something like:

    [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
    

    That will tell it to keep the callback valid for future use.

    Then do something like:

    [self.commandDelegate sendPluginResult:pluginResult callbackId:myCallbackId];
    
    0 讨论(0)
  • 2021-01-13 10:51

    hi for getting many callback to js you can use setKeepCallback(true)

    eg

     PluginResult p3=   new PluginResult(PluginResult.Status.OK, "0");
     p3.setKeepCallback(true);
    
    0 讨论(0)
提交回复
热议问题