gpt4 book ai didi

javascript - 在 iOS 4.0.0 中从 Native 调用 Cordova 插件 Javascript 函数

转载 作者:行者123 更新时间:2023-11-30 12:06:31 24 4
gpt4 key购买 nike

我正在尝试从 cordova iOS 4.0.0 中的 native 插件调用 javascript 函数。在之前的 cordova ios 版本 3.9.2 中,我可以像这样从 native 插件调用任何 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 函数?

最佳答案

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

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

之后 setPlayStatus() 函数被调用。

关于javascript - 在 iOS 4.0.0 中从 Native 调用 Cordova 插件 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35066122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com