【问题标题】:Phonegap - Send message to Javascript from Objective-c in a plugin delegatePhonegap - 在插件委托中从 Objective-c 向 Javascript 发送消息
【发布时间】:2013-07-25 19:53:12
【问题描述】:

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

我无法使用 'stringByEvaluatingJavaScriptFromString' 将这些事件发送回 javascript 似乎它不会触发调用。

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

这是 .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 objective-c cordova


    【解决方案1】:

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

    - (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];
     }
    

    【讨论】:

    • 我不想在插件中调用 Obj-c 方法,我已经有了。我想从 Obj-c 方法中调用 JS 函数。我看到你使用 [self writeJavascript:javaScript];它对我有用,但只有一次,这很奇怪!
    • 当我将它放入 echo 方法时,它实际上是有效的。但是在另一种方法中,例如 downloadDidStartForChannelIdentifier,它不会
    • 您需要始终有一个回调 ID 才能发送/写入 javascript。这是从调用 js 方法接收的。我很确定回调 id 只有从 js 方法被触发到收到回调成功/错误时的生命周期。
    • 所以如果我理解得很好我不能调用 [self writeJavascript:jsString];插件中的任何地方?
    • @PeterPiper 抱歉,我的服务器需要循环。文章应该备份。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-14
      • 1970-01-01
      • 2011-03-30
      • 2015-12-19
      • 2018-04-05
      • 2011-09-21
      • 2012-08-06
      • 2011-07-26
      相关资源
      最近更新 更多