Cannot pass array to javascript function when writing plugin for iOS

Showing 1-3 of 3 messages
Cannot pass array to javascript function when writing plugin for iOS Khanh Nguyen 7/26/11 8:59 AM
I'm writing a phonegap plugin for iOS.
I need to pass array to my function in javascript file. However, in .m file, these arrays are not seen and understood. 
Following is the details of the problem. In test.js, I call test() function with 2 arrays and 1 string. 
In MyPlugin.m, in test() function, however, the number of arguments shown is only 1.

So, my question is how can I do to pass arrays to my function?

----------- plugin.js --------------------
function MyPlugin(){
};

MyPlugin.prototype.test = function(arg1, arg2, arg3){
   PhoneGap.exec('MyPlugin.test', arg1, arg2, arg3);
}

.....
------------------------------------------

---------------declare plugin----------------
function onDeviceReady() {
   myPlugin = window.plugins.plugin;
}
--------------------------------------------------

-----------test.js where function is called----------------
function testPlugin(){
   var arr1 = new Array(),
        arr2 = newArray(),
        text = 'sample string';
   myPlugin.test(arr1, arr2, text);
};
-----------------------------------------------------------------------

--------------MyPlugin.m--------------------------
-(void)test:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
   NSUInteger argc = [arguments count];
   NSLog(@"Number of arguments: %d", argc);     //output: Number of arguments: 1
   
   NSString *text = [arguments objectAtIndex:0]; 
   NSLog(@"%@", text);   //output: sample string
}
---------------------------------------------------------

Thanks,
--
Khanh Nguyen

Re: Cannot pass array to javascript function when writing plugin for iOS Becka11y 7/26/11 3:51 PM
Try using the new format for PhoneGap.exec which is documented in
PhoneGapLib\javascripts\core\phonegap.base.js.

PhoneGap.exec(successCallback, errorCallback, class, method, [arg1,
arg2, {"myArray": arrayObj, "myOtherObj": otherObj}]);

successCallback and errorCallback can be null if there is no
callback.  Pass the arguments in an array. An object within the array
will be passed within the options dictionary.  The other arguments
will be in the arguments array. Arrays should be passed as part of an
object and then be retrieved via the options dictionary.
Here is an example for contacts.find() in contacts.js:
PhoneGap.exec(successCB, errorCB, "com.phonegap.contacts","search",
[{"fields":fields, "findOptions":options}]);

Note that fields is an array object and is passed as an object and
will be retrieved on the iOS side via from the options dictionary.

-becky


On Jul 26, 11:59 am, Khanh Nguyen <chepukhas...@gmail.com> wrote:
> I'm writing a phonegap plugin for iOS.
> I need to pass array to my function in javascript file. However, in .m file,
> these arrays are not seen and understood.
> Following is the details of the problem. In test.js, I call test() function
> with 2 arrays and 1 string.
> In MyPlugin.m, in test() function, however, the number of arguments shown is
> only 1.
>
> So, my question is how can I do to pass arrays to my function?
>
> ----------- *plugin.js* --------------------
> function MyPlugin(){
>
> };
>
> MyPlugin.prototype.*test* = function(arg1, arg2, arg3){
>    *PhoneGap.exec*('MyPlugin.test', arg1, arg2, arg3);
>
> }
>
> .....
> ------------------------------------------
>
> ---------------declare plugin----------------
> function onDeviceReady() {
>    *myPlugin* = window.plugins.plugin;}
>
> --------------------------------------------------
>
> -----------test.js where function is called----------------
> function testPlugin(){
>    var arr1 = new Array(),
>         arr2 = newArray(),
>         text = 'sample string';
>    *myPlugin.test(arr1, arr2, text);*};
>
> -----------------------------------------------------------------------
>
> --------------MyPlugin.m--------------------------
> -(void)test:(NSMutableArray*)arguments
> withDict:(NSMutableDictionary*)options
> {
>    NSUInteger argc = [arguments count];
>    NSLog(@"Number of arguments: %d", argc);     //output: *Number of
> arguments: 1*
>
>    NSString *text = [arguments objectAtIndex:0];
>    NSLog(@"%@", text);   //output:* sample string*}
>
> ---------------------------------------------------------
>
> Thanks,
> --
> *Khanh Nguyen*
Re: Cannot pass array to javascript function when writing plugin for iOS chepukha 7/27/11 9:52 AM
Thank you so much for your response, Becky.

 I've changed my code according to your suggestion and the example in
PhoneGapLib but I still have some problem. It's probably the way I
call my function. But I didn't figure out yet. Could you please
explain me where it's wrong? Below is the excerpt of my code.

 I put a few console.log() to see how it works. When my testPlugin is
called, I saw the message "Calling test function" in debugger, and
nothing else is shown. I have another console.log() in prototype.test,
but it's never shown.

 Thank you,

------in plugin.js----------------
MyPlugin.prototype.test = function(arg1, arg2, arg3){
   .....
   console.log("Execute test plugin");   //THIS IS NEVER SHOWN !!!!
   PhoneGap.exec(successCallback, errorCallback, 'MyPlugin', 'test',
[{"arr1":arg1, "arr2":arg2}], arg3);
}

-----------in test.js------------------
function testPlugin(){
   var arr1 = new Array(),
        arr2 = newArray(),
        text = 'sample string';
   console.log("Calling test function");  //ONLY THIS MESSAGE IS SHOWN
   myPlugin.test(arr1, arr2, text);
};

--------------MyPlugin.m--------------------------
-(void)test:(NSMutableArray*)arguments withDict:
(NSMutableDictionary*)options
{
   NSUInteger argc = [arguments count];
   NSString text = [arguments objectAtIndex:2];
   NSLog(@"%@", text);   //output: sample string
   NSArray *arr1 = [options valueForKey:@"arr1"];
   NSArray *arr2 = [options valueForKey:@"arr2"];
}