【发布时间】: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