开发者

How to pass array as argument to a function using phonegap plugin for iOS

开发者 https://www.devze.com 2023-03-22 11:31 出处:网络
I\'m writing a phonegap plugin for iOS. In javascript file, I need to pass some arrays to my function. However, in the .m file, [arguments count] only shows me the number of \'string\' arguments that

I'm writing a phonegap plugin for iOS. In javascript file, I need to pass some arrays to my function. However, in the .m file, [arguments count] only shows me the number of 'string' arguments that I passed to my function. That means, the arrays passed to my function are not understood/seen in the .m file.

Following is the senario:

 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.

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

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

//.....code is omitted......
------------------------------------------

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

So my question is how can 开发者_Python百科I pass arrays to javascript function in phonegap plugin for iOS.

Thanks


My solution for this problem is that I stringify the arrays, and pass them to the function as string. Then in .m file, I parse these strings into arrays.

That solves the problem. But if you know of any other solutions, please do recommend.

THanks,


I stumbled onto this same problem, and found another solution here:

https://groups.google.com/forum/?fromgroups#!topic/phonegap/Agy_9r_7FAc

Objects/arrays that are passed to from the cordova.exec function to the native iOS code, are stored in the options array parameter. Regular arguments like strings, integers, etc. are stored in the arguments array parameter.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号