Vincular um evento nativo do iOS a um webView usando um plug-in Cordova personalizado
Preciso criar um plug-in para capturar eventos que ocorrem no Cordova webView do meu aplicativo iOS e acionar ações na parte nativa do aplicativo e vice-versa.
Eu seguieste tutorial e funciona perfeitamente.
Quando tento adaptá-lo a outro aplicativo (eu queria que fosse mais geral do que o tutorial), ele funciona do webView para a parte nativa, mas não o contrário.
Estou apenas tentando clicar em um botão em uma barra de navegação para alterar a cor de fundo do meu webView. No momento, parece que o código javascript do plug-in não recebe o evento, porque apenas os logs do iOS são exibidos.
Todo o meu código está no XCode, portanto não vejo nenhum aviso / erro da Web Part.
Aqui está a parte do iOS do plugin:
@interface HybridBridge()
@property (nonatomic, retain) NSString *listenerCallbackId;
@end
@implementation HybridBridge
-(void)bindAction:(CDVInvokedUrlCommand*) command{
self.listenerCallbackId = command.callbackId;
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[pluginResult setKeepCallbackAsBool:true];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
-(void)reportEvent:(NSDictionary*)eventData{
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:eventData];
[pluginResult setKeepCallbackAsBool:true];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.listenerCallbackId];
}
Aqui está a parte javascript do plug-in:
var HybridBridge = (function() {
var PLUGIN_NAME = "HybridBridge";
var ACTION_BIND_LISTENER = "bindAction";
this.bindListener = function(listener) {
cordova.exec(listener, listener, PLUGIN_NAME, ACTION_BIND_LISTENER, []);
};
return this;
}());
Aqui está o ouvinte de eventos javascript:
var NativeEventsListener = (function() {
this.onReceivedEvent = function(eventData) {
var eventHandler = function(){};
switch (eventData.eventType){
case "colorButtonClicked":
eventHandler = colorButtonClickEvent;
break;
default:
}
eventHandler(eventData);
};
function colorButtonClickEvent(eventData){
changeBackgroundColor(eventData.color);
}
function changeBackground(color) {
document.body.style.background = color;
}
return this;
}());
Aqui está o arquivo main.js. onde vincular o ouvinte de eventos ao plug-in:
function wlCommonInit(){
HybridBridge.bindListener(NativeEventsListener.onReceivedEvent);
}
function wlEnvInit(){
wlCommonInit();
}
E finalmente, aqui está a chamada do plugin no objetivo-C:
- (IBAction)changeWebPageColor {
redColor = !redColor;
NSString *color = redColor ? @"red" : @"white";
HybridBridge *bridge = [self.pluginObjects objectForKey:@"HybridBridge"];
NSDictionary *eventData = [NSDictionary dictionaryWithObjectsAndKeys:
@"colorButtonClicked", @"eventType",
color, @"color",
nil];
[bridge reportEvent:eventData];
}
Obrigado pela ajuda!