Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass callback value to JavaScript form NATIVE CODE for iOS?

I am creating phoneGap plugin for iOS, which draws signature using NativeCode and Pass call back value to JavaScript.

I'm able to draw the signature succesfully using natice code but I'm not able to do pass return value of saved signature, to JavaScript.

JavaScript code is as follow

var MyPlugin = {
 nativeFunction: function(types, success, fail) {
      return Cordova.exec(success, fail, "MyPlugin", "print", types);
 }
};

In "print" function I am calling a UIViewContoller class which draws signature, and on SAVE button I want to pass return value to JavaScript.

Code on SAVE button click

// The first argument in the arguments parameter is the callbackID.
// We use this to send data back to the successCallback or failureCallback
// through PluginResult
self.callbackID = [arguments pop];

// Get the string that javascript sent us
NSString *stringObtainedFromJavascript = [arguments objectAtIndex:0];                 

// Create the Message that we wish to send to javascript   
NSMutableString *stringToReturn = [NSMutableString stringWithString: @"StringReceived:"];

// Append the received string to the string we plan to send out        
[stringToReturn appendString: stringObtainedFromJavascript];

// Create Plugin Result 
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
                                                  messageAsString: [stringToReturn stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

// Checking if the string received is HelloWorld or not    
 if ([stringObtainedFromJavascript isEqualToString:@"SAVED"] == YES)    
 {   
 // Call the javascript success function
 [self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
 }else    
 {    
 // Call the javascript error function
 [self writeJavascript: [pluginResult toErrorCallbackString:self.callbackID]];
 }

I am not getting any error or any exception.

Can anyone please help me out. Any suggetion will be appreciated.

Thanks in Advance.

like image 406
iLearner Avatar asked Aug 08 '26 19:08

iLearner


1 Answers

Take a look at this GitHub project: https://github.com/rohdef/PGPlugins

There are some good examples of writing plugins for phonegap.

like image 73
Christian Kuetbach Avatar answered Aug 11 '26 11:08

Christian Kuetbach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!