Skip to content

Instantly share code, notes, and snippets.

@dhavaln
Created January 25, 2012 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhavaln/1675595 to your computer and use it in GitHub Desktop.
Save dhavaln/1675595 to your computer and use it in GitHub Desktop.
PhoneGap iOS Plugin Sample
#import <Foundation/Foundation.h>
#ifdef PHONEGAP_FRAMEWORK
#import <PhoneGap/PGPlugin.h>
#else
#import "PGPlugin.h"
#endif
@interface AppComm : PGPlugin {
NSString* callbackID;
}
@property (nonatomic, copy) NSString* callbackID;
- (void) print: (NSMutableArray*) arguments withDict:(NSMutableDictionary*) options;
- (void) remoteCall: (NSMutableArray*) arguments withDict:(NSMutableDictionary*) options;
- (void) remotePost: (NSMutableArray*) arguments withDict:(NSMutableDictionary*) options;
@end
var AppCommControl = function() {
};
AppCommControl.prototype.print = function(data, successCallback, failureCallback) {
return PhoneGap.exec( successCallback, //Success callback from the plugin
failureCallback, //Error callback from the plugin
'AppComm', //Tell PhoneGap to run Plugin
'remoteCall', //Tell plugin, which action we want to perform
data); //Passing list of args to the plugin
};
AppCommControl.prototype.remoteCall = function(url, successCallback, failureCallback) {
return PhoneGap.exec( successCallback, //Success callback from the plugin
failureCallback,
'AppComm',
'remoteCall',
[url]);
};
AppCommControl.prototype.remotePost = function(url, data, successCallback, failureCallback) {
return PhoneGap.exec( successCallback, //Success callback from the plugin
failureCallback,
'AppComm',
'remotePost',
[url, data]);
};
AppCommControl.install = function(){
if(!window.plugins){
window.plugins = {};
}
window.plugins.appcomm = new AppCommControl();
return window.plugins.appcomm;
};
PhoneGap.addConstructor(AppCommControl.install());
#import "AppComm.h"
@implementation AppComm
@synthesize callbackID;
- (void) print: (NSMutableArray*) arguments withDict: (NSMutableDictionary*) options
{
self.callbackID = [arguments pop];
NSString *stringObtainedFromJavascript = [arguments objectAtIndex:0];
NSMutableString *stringToReturn = [NSMutableString stringWithString:@"StringReceived:"];
[stringToReturn appendString:stringObtainedFromJavascript];
PluginResult* pluginResult = [PluginResult resultWithStatus:PGCommandStatus_OK messageAsString: [stringToReturn stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if ([stringObtainedFromJavascript isEqualToString:@"HelloWorld"] == YES) {
[self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
}else {
[self writeJavascript: [pluginResult toErrorCallbackString:self.callbackID]];
}
}
- (void) remoteCall: (NSMutableArray*) arguments withDict: (NSMutableDictionary*) options
{
self.callbackID = [arguments pop];
PluginResult* pluginResult = [PluginResult resultWithStatus:PGCommandStatus_OK];
[self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
}
- (void) remotePost: (NSMutableArray*) arguments withDict: (NSMutableDictionary*) options
{
self.callbackID = [arguments pop];
PluginResult* pluginResult = [PluginResult resultWithStatus:PGCommandStatus_OK];
[self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
}
@end
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta charset="utf-8">
<script type="text/javascript" charset="utf-8" src="phonegap-1.3.0.js"></script>
<script type="text/javascript" src="appcomm.js"></script>
<script type="text/javascript">
function printSuccess(){
try{
window.plugins.appcomm.print(["HelloWorld"], function(result){
alert("Success: " + result);
},
function(error){
alert("Error: " + error);
});
}catch(e){
alert("call error : " + e);
}
}
function printError(){
try{
window.plugins.appcomm.print(["Howdy"], function(result){
alert("Success: " + result);
},
function(error){
alert("Error: " + error);
});
}catch(e){
alert("call error : " + e);
}
}
</script>
</head>
<body >
<input type="button" onclick="printSuccess()" value="Return Success">
<input type="button" onclick="printError()" value="Return Error">
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>TopActivityIndicator</key>
<string>gray</string>
<key>EnableLocation</key>
<false/>
<key>EnableViewportScale</key>
<true/>
<key>AutoHideSplashScreen</key>
<true/>
<key>ShowSplashScreenSpinner</key>
<true/>
<key>MediaPlaybackRequiresUserAction</key>
<false/>
<key>AllowInlineMediaPlayback</key>
<false/>
<key>OpenAllWhitelistURLsInWebView</key>
<true/>
<key>ExternalHosts</key>
<array>
<string>*</string>
</array>
<key>Plugins</key>
<dict>
<key>com.phonegap.accelerometer</key>
<string>PGAccelerometer</string>
<key>com.phonegap.camera</key>
<string>PGCamera</string>
<key>com.phonegap.connection</key>
<string>PGConnection</string>
<key>com.phonegap.contacts</key>
<string>PGContacts</string>
<key>com.phonegap.debugconsole</key>
<string>PGDebugConsole</string>
<key>com.phonegap.file</key>
<string>PGFile</string>
<key>com.phonegap.filetransfer</key>
<string>PGFileTransfer</string>
<key>com.phonegap.geolocation</key>
<string>PGLocation</string>
<key>com.phonegap.notification</key>
<string>PGNotification</string>
<key>com.phonegap.media</key>
<string>PGSound</string>
<key>com.phonegap.mediacapture</key>
<string>PGCapture</string>
<key>com.phonegap.splashscreen</key>
<string>PGSplashScreen</string>
<!-- New Plugin -->
<key>AppComm</key>
<string>AppComm</string>
</dict>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment