Keep callback context in PhoneGap plugin?

后端 未结 3 909
深忆病人
深忆病人 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: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];
    

提交回复
热议问题