如何使用phonegap插件

发布于 2024-12-26 08:00:35 字数 198 浏览 1 评论 0原文

我是phonegap开发的新手。我为 iphone 创建了一个插件类,其中包含一些加密方法。我想使用 .h 和 .m 文件来加密我的数据并从 html 页面中的类文件中获取数据。但我不知道如何在我的 javascript 文件中调用该类函数作为我的页面的结果。请任何人知道如何在 iPhone 应用程序的 javascript 中调用任何类文件及其方法,然后帮助我。我正在等待回复。

I am new in phonegap development . I have created one plugin class for iphone which contains some method for encryption. I want to use that .h and .m file to encrypt my data and get the data from my class file in my html page. But i don't have any idea how to call that class function in my javascript file as a result of my page. Please any one have idea how to call any class file and its method in javascript for iphone application then help me .I am waiting for reply.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷爱 2025-01-02 08:00:35

请参阅保罗回答的链接。为了快速运行,有几种方法可以做到这一点,但我的通常会得到这样的结果。

Javascript:

PhoneGap.exec("NameOfObjectiveCFile.nameofMethod","parameter1", "parameter2");

Objective C:

-(void)nameOfMethod:(NSMutableArray*)paramArray withDict: (NSMutableDictionary*) options
    {
       //Do your stuff
       //Access your params you sent in javascript by doing the following
       NSString *parameter1 = [paramArray objectAtIndex:0];
       //Send stuff back to Javascript using a callback function
       NSString *jsCallBack = [NSString stringWithFormat:@"nameofJavascriptFinishedMethod(%@)",parameterToSendBack];
       [self.webview stringByEvaluatingJavaScriptFromString:jsCallBack];

    }

回到 Javascript:

function nameofJavascriptFinishedMethod(parameterFromObjectiveC)
{
   //Do stuff with information from objective C
}

还要记住您需要在 Phonegap.plist 中注册您的插件
希望这有帮助,祝你好运。

更新:如果您希望 html 将信息发送到您的插件,我会使用 html 表单或按钮或触发一个操作来调用您的 javascript 函数(上面的第一个)并传递您的变量从你的田地里采集来的。请参阅此链接了解表单基础知识。

更新2
1)在xcode中创建新的phonegap项目并构建以获取www文件夹
2)将现有的加密目标 C 文件添加到项目
3)创建新的目标C类,将其命名为EncryptPlugin(下一步参见步骤5)
4)编辑PhoneGap.plist文件
a) 在插件下添加一个新条目
b) 将其命名为 EncryptPlugin String EncryptPlugin
5) EncryptPlugin 的头文件应如下所示

#import <Foundation/Foundation.h>
#import <UIKit/UIkit.h>

#ifdef PHONEGAP_FRAMEWORK
#import <PhoneGap/PGPlugin.h>
#else
#import "PGPlugin.h"
#endif

@interface EncryptPlugin : PGPlugin {



}
-(void) useMyEncryptionFiles:(NSMutableArray*)paramArray withDict:(NSMutableDictionary*)options;
@end

6) 实现文件应如下所示

#import "EncryptPlugin.h"
#import "EncryptPacket.h"


@implementation EncryptPlugin


//phonegap magic -- basically ignore
-(PGPlugin*) initWithWebView:(UIWebView*)theWebView
{
    self = (PdfCreator*)[super initWithWebView:theWebView];
    return self;
}

//The method that your javascript is going to call
-(void) useMyEncryptionFiles:(NSMutableArray*)paramArray withDict:(NSMutableDictionary*)options
{
    //This gives you your string
    NSString *stringFromJavascript = [paramArray objectAtIndex:0];

    EncryptPacket *ep = [[EncryptPacket alloc] init];

    NSString *encryptedString = [ep encryptRequest:stringFromJavascript];

    NSLog(@"encryptedString = %@",encryptedString);
}




@end

7) 在您的 html javascript 中使用类似以下内容调用函数
p1.html

<head><script type="text/javascript" src="encryptdata.js"></script></head>
<body>
<input type="password" name="confirmPassword" id="confirmPassword" value="" />
<input type="button" value="FetchR" onclick="fetchRelation()"/>
</body> 



encryptdata.js  
function fetchRelation()
{
    var getvalue=document.getElementById('confirmPassword').value;

    //what is next step....... to send the data to the plugin class
    PhoneGap.exec("EncryptPlugin.useMyEncrptionFiles",getValue);
}

这应该可以帮助您入门。如果您想将内容发送回您的 html javascript,请使用我上面指定的函数:

NSString *jsCallBack = [NSStringstringWithFormat:@"nameofJavascriptFinishedMethod(%@)",parameterToSendBack];
           [self.webview stringByEvaluatingJavaScriptFromString:jsCallBack];

希望这会有所帮助,您应该能够通过这些说明弄清楚它。它们可能并不完美,但我没有时间将它们放在一起并使用您的代码进行编译。祝你好运。

See the link that Paul answered. For a quick run down, there are a couple ways to do this, but mine usually end up something like this.

Javascript:

PhoneGap.exec("NameOfObjectiveCFile.nameofMethod","parameter1", "parameter2");

Objective C:

-(void)nameOfMethod:(NSMutableArray*)paramArray withDict: (NSMutableDictionary*) options
    {
       //Do your stuff
       //Access your params you sent in javascript by doing the following
       NSString *parameter1 = [paramArray objectAtIndex:0];
       //Send stuff back to Javascript using a callback function
       NSString *jsCallBack = [NSString stringWithFormat:@"nameofJavascriptFinishedMethod(%@)",parameterToSendBack];
       [self.webview stringByEvaluatingJavaScriptFromString:jsCallBack];

    }

Back to Javascript:

function nameofJavascriptFinishedMethod(parameterFromObjectiveC)
{
   //Do stuff with information from objective C
}

Also remember you need to register your plugin in your Phonegap.plist
Hope this helps, good luck.

Update: If you want your html to send information to your plugin, i'd use an html form or button or trigger an action that will call your javascript function (The first one above) and pass your variables gathered from your fields. See this link for form basics.

Update 2
1)Create new phonegap project in xcode and build to get www folder
2)Add your existing Encryption objective C files to project
3)Create new objective C class, call it EncryptPlugin (See step 5 for next)
4)Edit PhoneGap.plist file
a)add a new entry under Plugins
b)name it EncryptPlugin String EncryptPlugin
5)Header file for EncryptPlugin should look like this

#import <Foundation/Foundation.h>
#import <UIKit/UIkit.h>

#ifdef PHONEGAP_FRAMEWORK
#import <PhoneGap/PGPlugin.h>
#else
#import "PGPlugin.h"
#endif

@interface EncryptPlugin : PGPlugin {



}
-(void) useMyEncryptionFiles:(NSMutableArray*)paramArray withDict:(NSMutableDictionary*)options;
@end

6)Implementation file should look like this

#import "EncryptPlugin.h"
#import "EncryptPacket.h"


@implementation EncryptPlugin


//phonegap magic -- basically ignore
-(PGPlugin*) initWithWebView:(UIWebView*)theWebView
{
    self = (PdfCreator*)[super initWithWebView:theWebView];
    return self;
}

//The method that your javascript is going to call
-(void) useMyEncryptionFiles:(NSMutableArray*)paramArray withDict:(NSMutableDictionary*)options
{
    //This gives you your string
    NSString *stringFromJavascript = [paramArray objectAtIndex:0];

    EncryptPacket *ep = [[EncryptPacket alloc] init];

    NSString *encryptedString = [ep encryptRequest:stringFromJavascript];

    NSLog(@"encryptedString = %@",encryptedString);
}




@end

7)In your html javascript call the function using something like this
p1.html

<head><script type="text/javascript" src="encryptdata.js"></script></head>
<body>
<input type="password" name="confirmPassword" id="confirmPassword" value="" />
<input type="button" value="FetchR" onclick="fetchRelation()"/>
</body> 



encryptdata.js  
function fetchRelation()
{
    var getvalue=document.getElementById('confirmPassword').value;

    //what is next step....... to send the data to the plugin class
    PhoneGap.exec("EncryptPlugin.useMyEncrptionFiles",getValue);
}

This should get you started. If you want to send stuff back to your html javascript then use the function I specified above:

NSString *jsCallBack = [NSStringstringWithFormat:@"nameofJavascriptFinishedMethod(%@)",parameterToSendBack];
           [self.webview stringByEvaluatingJavaScriptFromString:jsCallBack];

Hopefully this helps, you should be able to figure it out with these instructions. They probably aren't perfect, but I didn't have time to put it all together and compile it using your code. Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文