从iOS 4.0.0中的Native调用Cordova插件Javascript函数 [英] Cordova Plugin Javascript Function call from Native in iOS 4.0.0

查看:116
本文介绍了从iOS 4.0.0中的Native调用Cordova插件Javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Cordova iOS 4.0.0中的本机插件调用javascript函数。
在以前的cordova ios版本3.9.2中,我可以从本地插件中调用任何javascript函数,如下所示。

I am trying to call javascript function from native plugin in cordova iOS 4.0.0. In the previous cordova ios version 3.9.2, I can call any javascript function from native plugin like this.

- (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 。所以我试图这样。

But in Cordova iOS 4.0.0, I can't call javascript function setPlayStatus() like as above. Because viewController.webview is not UIWebView type. It is UIView. So I was tried to like this.

- (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函数?我已经做到了。 CDVPlugin 有一个 commandDelegate 。此 commandDelegate 可以使用 evalJS 函数调用 javascript 函数。
所以我这样尝试。

Thank you everybody. I have done it.CDVPlugin has a commandDelegate. This commandDelegate can call javascript function with evalJS function. So I tried like this.

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

之后, setPlayStatus()函数是

这篇关于从iOS 4.0.0中的Native调用Cordova插件Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆