cordova默认只能回调一次,回调一次后会被销毁,再回调js就收不到回调
方法:将 CDVPluginResult.keepCallback 设置为 true ,则不会销毁callback
CDVPluginResult* pluginResult;
NSMutableDictionary* successDictionary = [NSMutableDictionary dictionaryWithCapacity:3];
[successDictionary setObject:[NSString stringWithFormat:@"%f",_playerView.currentTime] forKey:@"position"];
[successDictionary setObject:[NSString stringWithFormat:@"%f",_playerView.videoAllTime] forKey:@"duration"];
[successDictionary setObject:[NSString stringWithFormat:@"%@",_videoid] forKey:@"videoid"];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:successDictionary];
//将 CDVPluginResult.keepCallback 设置为 true ,则不会销毁callback
[pluginResult setKeepCallbackAsBool:YES];
[self.plugin.commandDelegate sendPluginResult:pluginResult callbackId:self.callback];
简单解析下多次callback原理
cordova.js 文件中:
// 接受原生callback方法
callbackFromNative: function(callbackId, isSuccess, status, args, keepCallback) {
try {
var callback = cordova.callbacks[callbackId];
// 首先判断callback是否存在,存在则调用回调方法
if (callback) {
if (isSuccess && status == cordova.callbackStatus.OK) {
callback.success && callback.success.apply(null, args);
} else if (!isSuccess) {
callback.fail && callback.fail.apply(null, args);
}
// 如果这里没有设置keepcallback,则默认会把callbackid从callback数组中移除,下次就不会触发回调方法
if (!keepCallback) {
delete cordova.callbacks[callbackId];
}
}