Phonegap - 从一个插件代理中的Objective-c发送消息到Javascript [英] Phonegap - Send message to Javascript from Objective-c in a plugin delegate
问题描述
我有一个Phonegap Cordova插件。在这个插件中我收到一个来自javascript的点击事件。这个点击触发使用我的客户端库文件下载。
此文件下载在我的插件中发送事件和调用方法,因为我已将其设置为委托。
I have a Phonegap Cordova plugin. In this plugin I receive a click event from javascript.This click triggers a file download using my client library. This file download sends events and calls methods in my plugin, as I have set it to be delegate.
我无法管理发送这些事件到javascript使用'stringByEvaluatingJavaScriptFromString'它似乎不会触发调用。
I can't manage to sends those events back to the javascript using 'stringByEvaluatingJavaScriptFromString' It seems like it doesn't triggers the call.
这是工作时,我试图调用它后javascript单击echo Plugin方法。
It's working when I try to call it after a javascript click to the echo Plugin method.
以下是.m插件类:
#import "CCDataBundlePlugin.h"
#import <Cordova/CDV.h>
@implementation CCDataBundlePlugin
-(id)init{
[MYCLIENTLIB sharedInstance].delegate = self;
self = [super init];
return self;
}
- (void)echo:(CDVInvokedUrlCommand*)command
{
NSLog(@"------ Received click");
CDVPluginResult* pluginResult = nil;
NSString* echo = [command.arguments objectAtIndex:0];
if (echo != nil && [echo length] > 0) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
// When latest extraction succeeded, call next download
[[MYCLIENTLIB sharedInstance] downloadBundlesForChannel:@"myChannel"];
// When latest download succeeded, call next extraction
[[MYCLIENTLIB sharedInstance] extractBundlesForChannel:@"myChannel"];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
-(void)callJavascriptMethodWithString:(NSString*)stringParameter{
NSString* jsString = [NSString stringWithFormat:@"CN.Interface.Recepter.receiveWithString(\"%@\");", stringParameter];
//The line under is not working, I don't get the function call in JS
[self.webView stringByEvaluatingJavaScriptFromString:jsString];
}
// DOWNLOAD EVENTS Called from the Library
- (void)dataBundle:(MYCLIENTLIB *)dataBundle downloadDidStartForChannelIdentifier:(NSString *)channelIdentifier {
NSLog(@"download in progress..."); // this is logged
[self callJavascriptMethodWithString:@"download in progress..."];
// The line above do calls the method
}
推荐答案
只要有人想做同样的事情,我设法通过使用javascript定时器调用本地插件方法。我会搜索如果我能找到一个更清洁的方法
Just If someone want to do the same, I managed to do it by calling a native plugin method using a javascript timer. I'll search If I can find a cleaner way
这篇关于Phonegap - 从一个插件代理中的Objective-c发送消息到Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!