保持iOS Cordova插件变量的监听器

时间:2016-08-19 10:02:25

标签: ios cordova cordova-plugins

我正在构建一个iOS Cordova插件,在这个插件中我有一个总是在改变值的变量。 当我调用这个从js返回此变量的插件方法时,我希望它保持活动状态并始终从Objective-c获取新的更改值。

这是我的代码:

/**
 *This method will return the Volume of the user's speech ( It can be used as a UI feedback)
 */
- (void) getRecognitionVolume:(CDVInvokedUrlCommand*)command
{
    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[NSString stringWithFormat:@"%1.6f", volumeLevel]];

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

    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

js code:

getVolumeBtn.addEventListener('click', function() {

                                    cordova.exec(
                                                 function successCallback(data) {
                                                 volumeDiv.innerHTML ="Volume: "+ data;
                                                 },
                                                 function errorCallback(err) {
                                                 alert('Error');
                                                 },
                                                 'VoiceControl',
                                                 'getRecognitionVolume',
                                                 []
                                                 );
                                    });

总而言之,我希望volumeDiv始终保持新的音量值。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

问题是我没有完全理解setKeepCallback的工作方式: 我认为如果设置为true,它将自动保持方法运行并在变量发生变化时发送新值。

我必须使用它的方式是每当值发生变化时我必须再次发送结果。

这是我现在传递结果的地方: (这是我的代码中卷更改时始终调用的处理程序)

- (void) pollVolume
{
   [self getRecognitionVolume:volumeCommand];

}