3

I have a Phonegap Cordova plugin. In this plugin I receive a click event from javascript.This click triggers a file download using my client library. This file download sends events and calls methods in my plugin, as I have set it to be delegate.

I can't manage to sends those events back to the javascript using 'stringByEvaluatingJavaScriptFromString' It seems like it doesn't triggers the call.

It's working when I try to call it after a javascript click to the echo Plugin method.

Here is the .m plugin class :

#import "CCDataBundlePlugin.h"
#import <Cordova/CDV.h>

@implementation CCDataBundlePlugin


-(id)init{

[MYCLIENTLIB sharedInstance].delegate = self;

self = [super init];
    return self;
}

- (void)echo:(CDVInvokedUrlCommand*)command
{
NSLog(@"------ Received click");
    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];
    }

// When latest extraction succeeded, call next download
[[MYCLIENTLIB sharedInstance] downloadBundlesForChannel:@"myChannel"];
// When latest download succeeded, call next extraction
[[MYCLIENTLIB sharedInstance] extractBundlesForChannel:@"myChannel"];

    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

-(void)callJavascriptMethodWithString:(NSString*)stringParameter{

NSString* jsString = [NSString stringWithFormat:@"CN.Interface.Recepter.receiveWithString(\"%@\");", stringParameter];

    //The line under is not working, I don't get the function call in JS
[self.webView stringByEvaluatingJavaScriptFromString:jsString];

}

// DOWNLOAD EVENTS Called from the Library
- (void)dataBundle:(MYCLIENTLIB *)dataBundle downloadDidStartForChannelIdentifier:(NSString *)channelIdentifier  {

NSLog(@"download in progress..."); // this is logged

[self callJavascriptMethodWithString:@"download in progress..."];
    // The line above do calls the method

}

2 Answers 2

1

I wrote a cordova plugin tutorial not to long ago here: http://www.zen-sign.com/writing-a-cordova-plugin/. It should be a good help to you. In the meantime, here is the basic structure i have used with success inside my init/congifure methods for my native-side plugin wrapper:

- (void)configurePlugin:(CDVInvokedUrlCommand*)command
{
      // Cordova Result object and stirng to send to JS
      CDVPluginResult* pluginResult = nil;
      NSString* javaScript = nil;

      @try
      {
           // Create an instance of the class we want to wrap. This could be your 3rd party    library or whatever you want.
           wrappedClass = [[WrapMeUp alloc] init];

           pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"{'success' : '1'}"];
           javaScript = [pluginResult toSuccessCallbackString:command.callbackId];

      }
      @catch (NSException* exception)
      {
           //Sumptin went worng!
           pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[exception reason]];
           javaScript = [pluginResult toErrorCallbackString:command.callbackId];
      }

      // Write plugin results back to JS callback
      [self writeJavascript:javaScript];
 }
Sign up to request clarification or add additional context in comments.

7 Comments

I don't want to have something to call Obj-c methods in a plugin, I already have that. I want to call JS functions from Obj-c methods. I've seen that you use [self writeJavascript:javaScript]; It's working for me, but only once, that's strange !
Actually its working when I put it into the echo method. But in another method, like downloadDidStartForChannelIdentifier, it doesnt
You need to always have a callback ID for it to send/write the javascript to. This is received from the calling js method. I'm pretty sure that callback id only has a lifespan form the time the js method is fired to when it receives a callback success/error.
So If I understand well I can't call [self writeJavascript:jsString]; anywhere in the plugin ?
@PeterPiper Sorry, my server needed cycled. The article should be back up.
|
1

Just If someone want to do the same, I managed to do it by calling a native plugin method using a javascript timer. I'll search If I can find a cleaner way

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.