Phonegap - 在插件委托中从Objective-c发送消息给Javascript

时间:2013-07-23 12:37:56

标签: javascript objective-c cordova

我有一个Phonegap Cordova插件。在这个插件中,我从javascript收到一个点击事件。这个点击触发使用我的客户端库下载文件。 此文件下载在我的插件中发送事件和调用方法,因为我已将其设置为委托。

我无法使用' stringByEvaluatingJavaScriptFromString'将这些事件发送回javascript。似乎它没有触发通话。

当我尝试在javascript点击echo插件方法后调用它时,它正在工作。

这是.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

}

2 个答案:

答案 0 :(得分:1)

我不久前在这里写了一篇cordova插件教程:http://www.zen-sign.com/writing-a-cordova-plugin/。这对你来说应该是一个很好的帮助。与此同时,这里是我在我的原生端插件包装器的init / congifure方法中成功使用的基本结构:

- (void)configurePlugin:(CDVInvokedUrlCommand*)command
{
      // Cordova Result object and stirng to send to JS
      CDVPluginResult* pluginResult = nil;
      NSString* javaScript = nil;

      @try
      {
           // Create an instance of the class we want to wrap. This could be your 3rd party    library or whatever you want.
           wrappedClass = [[WrapMeUp alloc] init];

           pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"{'success' : '1'}"];
           javaScript = [pluginResult toSuccessCallbackString:command.callbackId];

      }
      @catch (NSException* exception)
      {
           //Sumptin went worng!
           pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[exception reason]];
           javaScript = [pluginResult toErrorCallbackString:command.callbackId];
      }

      // Write plugin results back to JS callback
      [self writeJavascript:javaScript];
 }

答案 1 :(得分:1)

如果有人想要这样做,我设法通过使用javascript计时器调用本机插件方法来实现。我会搜索如果我能找到更清洁的方式