簡體   English   中英

Cordova Plugin Javascript函數調用來自iOS 4.0.0中的Native

[英]Cordova Plugin Javascript Function call from Native in iOS 4.0.0

我試圖從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插件iOS 4.0.0中調用本機cordova插件中的javascript函數?

謝謝大家。 我已經做了。 CDVPlugin有一個commandDelegate 這個commandDelegate可以用evalJS函數調用javascript函數。 所以我試過這樣的。

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

之后調用了setPlayStatus()函數。

我只是快速讀取科爾多瓦的iOS插件代碼,它看起來像self.viewController.webView持有經webEngine一個UIWebView。 它也許可以。

您應該在轉換之前使用isKindOfClass:進行檢查。

編輯:更好:引擎是一個協議,具有:

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

采用:

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

我已經嘗試過了,這對我有用

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

@Tim Rogers答案中的拼寫錯誤,它應該是evalJs而不是evalJS:

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

它適用於Cordova ios 4.1.1

抱歉,我沒有50個聲譽來添加評論,所以必須在這里添加新的答案

我遇到了同樣的問題,我嘗試在下面的代碼中從本機調用.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>

這適合我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM