【问题标题】:Cordova Plugin Javascript Function call from Native in iOS 4.0.0来自 iOS 4.0.0 中 Native 的 Cordova 插件 Javascript 函数调用
【发布时间】:2016-01-28 15:56:35
【问题描述】:

我正在尝试从 cordova iOS 4.0.0 中的本机插件调用 javascript 函数。 在之前的cordova ios 3.9.2版本中,我可以像这样从本机插件调用任何javascript函数。

- (void)PlayMp3:(CDVInvokedUrlCommand*)command
{       

    NSString* callbackId = [command callbackId];
    NSString* name = [[command arguments] objectAtIndex:0];
    NSString* msg = [NSString stringWithFormat: @"Hello, %@", name];

    CDVPluginResult* result = [CDVPluginResult
                               resultWithStatus:CDVCommandStatus_OK
                               messageAsString:msg];

    MainViewController* viewController = (MainViewController*)self.viewController;

    NSString *jsCallBack = [NSString stringWithFormat:@"setPlayStatus();"];
    [viewController.webView stringByEvaluatingJavaScriptFromString:jsCallBack];

    [self success:result callbackId:callbackId];
}

但在 Cordova iOS 4.0.0 中,我不能像上面那样调用 javascript 函数 setPlayStatus()。因为viewController.webview 不是UIWebView 类型。它是UIView。所以我试着喜欢这个。

- (void)PlayMp3:(CDVInvokedUrlCommand*)command
{
    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];
    }

    WKWebView* webview = (WKWebView*)self.viewController.view;

    NSString *jsCallBack = [NSString stringWithFormat:@"setPlayStatus();"];
   [webview evaluateJavaScript:@"setPlayStatus();" completionHandler:nil];

    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

但它没有用。如何在 Cordova Plugin iOS 4.0.0 中调用原生 cordova 插件中的 javascript 函数?

【问题讨论】:

  • 您遇到什么错误?是不是对象没有响应选择器evaluateJavaScript

标签: javascript ios cordova


【解决方案1】:

谢谢大家。我已经做到了。CDVPlugin 有一个commandDelegate。这个commandDelegate 可以用evalJS 函数调用javascript 函数。 所以我就这样尝试了。

[self.commandDelegate evalJS:@"setPlayStatus()"];

之后,setPlayStatus() 函数被调用。

【讨论】:

    【解决方案2】:

    我刚刚快速阅读了 Cordova iOS 插件代码,看起来 self.viewController.webView 通过 webEngine 持有一个 UIWebView。或许两者都可以。

    您应该在投射前使用isKindOfClass: 进行检查。

    编辑:更好:引擎是一个协议:

    - (void)evaluateJavaScript:(NSString*)javaScriptString completionHandler:(void (^)(id, NSError*))completionHandler;
    

    用途:

    [self.viewController.webViewEngine evaluateJavaScript:@"setPlayStatus();" completionHandler:nil]
    

    【讨论】:

      【解决方案3】:

      我自己试过了,这对我有用

      [self.webViewEngine evaluateJavaScript:[NSString stringWithFormat:@"funcName(%@)",yourparam]]
      

      【讨论】:

        【解决方案4】:

        @Tim Rogers 的回答中有错字,应该是 evalJs 而不是 evalJS:

        [self.commandDelegate evalJs:@"setPlayStatus()"];

        在 Cordova ios 4.1.1 上运行良好

        抱歉,我没有 50 个声望来添加评论,所以必须在此处添加新答案

        【讨论】:

          【解决方案5】:

          我遇到了同样的问题,我尝试使用下面的代码从本机调用 .js,然后我再次使用 Cordova.exec 发送参数

          [self.webViewEngine evaluateJavaScript:[NSString stringWithFormat:@"setPlayStatus()"];   
          

          您必须在 config.xml 中添加以下功能

          <feature name="setPlayStatus">
                  <param name="ios-package" value="setPlayStatus" />
                  <param name="onload" value="true" />
             </feature>
          

          这对我有用。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-01-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-03-10
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多