1

I need some Help regarding Phonegap plugins:

First , i have a JS File which invoke the native code using the phonegap.exec() containing the result handler function, error handler function , a reference to the native class's name and native function name as well as an array of parameters . My question is : if it is possible to invoke the function (native method) with given specified parameters?

That means : in my phonegap plugin file (.h & .m) 1- can i specify the arguments and their Types (NSInteger, NSString) like java void myMethod(int a , string b){}

-(void) myMethod:(NSMutableArray )arguments withDict:(NSMutableDictionary)options;

Or is it as specified by Phonegap or Objective C ?

2- And what does it withDict means in this case ?? 3- can i addicate this?

4- Why should my Code looks like this ?

-(void)myMethod: (NSMutableArray*)arguments withDict:(NSMutableDictionary*)options { NSString *callbackID =[arguments pop]; NSString *myParam = @"";

NSArray *arrayArguments = [[arguments objectAtIndex:0] componentsSeparatedByString:@"|"];
    NSString *stringArgument  = ([arArguments objectAtIndex:0]); 

I want to invoke my method like this :

why shall i put my arguments (as a String array element) then take it out , split it to get the right element from the String )?

Many Thanks for helping

4

1 回答 1

1

好的,这就是你的做法……我在下面添加的示例是 Phonegap 中电子邮件插件的实现,具有多个字符串。您可以随时替换我的字符串代码来识别 NSNumber 或任何其他类型的参数。

在 JS ::

首先,我用它们的值创建参数。. .

var attachmentData = {}; 
attachmentData.data = userData; 
attachmentData.fileName = fileName;
var mailData = {};
mailData.toRecipients = "";
mailData.subject = "Exporting Backup for user data";
mailData.body = "User data for application. Please find the attachment containing the data from the last week.";
nativeMail(attachmentData, mailData);

现在我们调用一个函数,为 Phonegap 插件打包所有这些数据并将其发送到插件类

function nativeMail(attachmentData, mailData){
    e_args = {};
    if(mailData.toRecipients)
    e_args.toRecipients = mailData.toRecipients; 
    if(mailData.subject)
    e_args.subject = mailData.subject; //"Hello World";
    if(mailData.body)
    e_args.body = mailData.body; 
    //call to phonegap plugin for native mail iOS
    e_args.attachmentFileName = attachmentData.fileName;
    e_args.datatoattach = attachmentData.data;
    cordova.exec(null, fail, "EmailComposer", "showEmailComposer", [e_args]);
}

现在是 EmailComposer.h 文件

- (void) showEmailComposer:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;

最后,如何从 Mutablle Dictionary/Array 中获取这些参数并检索我们的字符串值。

- (void) showEmailComposer:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{   
    NSString* toRecipientsString = [options valueForKey:@"toRecipients"];
    NSString* ccRecipientsString = [options valueForKey:@"ccRecipients"];
    NSString* bccRecipientsString = [options valueForKey:@"bccRecipients"];
    NSString* subject = [options valueForKey:@"subject"];
    NSString* body = [options valueForKey:@"body"];
    NSString* isHTML = [options valueForKey:@"bIsHTML"];
. . . . . . 
}

这是解决它的唯一方法。它与 Phonegap 本身处理从您的 javascript Web 应用程序传递到本机类的数据的方式有关。它不能改变。MutableDictionary 或 MutableArray 将处理您需要的任何类型的数据。没有限制。但是,只能使用上述格式传递此数据。一旦您在 .m 类中获得了选项和参数,您就可以自由地检索它们或将它们解析为您需要的数据类型。

于 2013-05-20T14:46:40.240 回答