Reputation: 723
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.
Upvotes: 0
Views: 3718
Reputation: 1977
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.
Upvotes: 3