Reputation: 14408
It been a day scratching my head for this setup. I m feeling writing a plugin code is much easier than the setup. I followed the below steps to to write a simple phone gap plugin for ios. But unfortunately, unable to locate where I missed. Kindly do printout the missing/confusing/erroneous part of the code.
PhoneGap Setup:
1) Closed Xcode. [ i have Xcode 4.3.3 ] 2) Downloaded latest phone gap. version 2.0 3) in phonegap-phonegap-2dbbdab-1 directory, install Cordova-2.0.0.pkg
4) Ran the following code:
$ ./path/to/cordova-ios/bin/create /path/to/my_new_cordova_project com.example.cordova_project_name CordovaProjectName
Followed: http://docs.phonegap.com/en/2.0.0/guide_command-line_index.md.html#Command-Line%20Usage
5) Opened Xcode project.
6) created HelloPlugin.js file under www directory that contains..
var HelloPlugin = {
callNativeFunction: function (success, fail, resultType) {
return Cordova.exec(success, fail, "com.tricedesigns.HelloPlugin", "nativeFunction", [resultType]);
}
};
7) IN PLUGIN DIRECTORY
HelloPlugin.h contains:
#import "CDVPlugin.h"
@interface HelloPlugin : CDVPlugin {
NSString* callbackID;
}
@property (nonatomic, copy) NSString* callbackID;
- (void) nativeFunction:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end
HelloPlugin.m contains:
#import "HelloPlugin.h"
@implementation HelloPlugin
@synthesize callbackID;
- (void) nativeFunction:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
//get the callback id
NSString *callbackId = [arguments pop];
NSLog(@"Hello, this i s a native function called from PhoneGap/Cordova!");
NSString *resultType = [arguments objectAtIndex:0];
CDVPluginResult *result;
if ( [resultType isEqualToString:@"success"] ) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @"Success :)"];
[self writeJavascript:[result toSuccessCallbackString:callbackId]];
}
else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: @"Error :("];
[self writeJavascript:[result toErrorCallbackString:callbackId]];
}
}
@end
in Cordova.plist: Have added com.tricedesigns.HelloPlugin, HelloPlugin as key/value and type is string.
in index.html:
<script type="text/javascript" charset="utf-8" src="HelloPlugin.js"></script>
<script type="text/javascript">
function callNativePlugin( returnSuccess ) {
alert("Inside callNativePlugin");
HelloPlugin.callNativeFunction( nativePluginResultHandler, nativePluginErrorHandler, returnSuccess );
alert("End of Hello PLugin");
}
function nativePluginResultHandler (result) {
alert("SUCCESS: \r\n"+result );
}
function nativePluginErrorHandler (error) {
alert("ERROR: \r\n"+error );
}
</script>
Now when i click the button, the native function is not invoked. How to move forward?
Upvotes: 2
Views: 8714