1

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];
}

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];
}

But it didn't work. How can I call javascript function in native cordova plugin in Cordova Plugin iOS 4.0.0?

1
  • What error are you getting? Is it that the object doesn't respond to the selector evaluateJavaScript ? Commented Jan 28, 2016 at 16:10

5 Answers 5

2

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()"];

After that the setPlayStatus() function was called.

Sign up to request clarification or add additional context in comments.

Comments

1

I just quickly read the Cordova iOS plugin code and it looks like self.viewController.webView is holding a UIWebView via the webEngine. It might be able to be either.

You should use isKindOfClass: to check before casting.

EDIT: Even better: the engine is a protocol that has this:

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

Use:

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

Comments

1

A typo in @Tim Rogers's answer, it should be evalJs instead of evalJS:

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

it works fine with Cordova ios 4.1.1

Sorry I don't have 50 reputations to add a comment so have to add a new answer here

Comments

0

I've tried myself and this works for me

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

Comments

0

I was facing same Issue where I tried below code to make .js call from native, and again I am using Cordova.exec to send parameters

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

below feature you have to add in config.xml

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

This works for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.