あいつの日誌β

働きながら旅しています。

How to create ios plugin into PhoneGap 3.0

Enviroments

% npm version
{ http_parser: '1.0',
  node: '0.10.18',
  v8: '3.14.5.9',
  ares: '1.9.0-DEV',
  uv: '0.10.15',
  zlib: '1.2.3',
  modules: '11',
  openssl: '1.0.1e',
  npm: '1.3.8' }

% phonegap -v
3.0.0-0.14.3

Tutorial

Create project

% cd ~
% phonegap create hello com.example.hello HelloWorld
% cd hello
% phonegap build ios 

edit platforms/ios/HelloWorld/config.xml

     <feature name="LocalStorage">
         <param name="ios-package" value="CDVLocalStorage" />
     </feature>
+    <feature name="Hello">
+        <param name="ios-package" value="Hello" />
+    </feature>
     <access origin="http://127.0.0.1*" />
     <preference name="KeyboardDisplayRequiresUserAction" value="true" />
     <preference name="SuppressesIncrementalRendering" value="false" />

Open HelloWorld.xcodeproj by xcode.

% open -a Xcode platforms/ios/HelloWorld.xcodeproj/

Add Hello.h and Hello.m subclass of CDVPlugin into Plugins directory and edit its.

// Hello.h

#import <Cordova/CDVPlugin.h>
#import "Foundation/Foundation.h"

@interface Hello : CDVPlugin {
}

- (void) hello:(CDVInvokedUrlCommand*)command;

@end
// Hello.m

#import "Cordova/CDV.h"
#import "Hello.h"

@implementation Hello

- (void) hello:(CDVInvokedUrlCommand*)command {
    NSLog(@"Hello, this is a native function called from PhoneGap/Cordova!");
}

@end

You have already implemented your plugin. Let's use it.

Edit www/js/index.js like this.

@@ -44,6 +44,11 @@ var app = {
         listeningElement.setAttribute('style', 'display:none;');
         receivedElement.setAttribute('style', 'display:block;');
 
+        document.addEventListener("touchstart", function touchHandler(e) {
+            e.preventDefault();
+            cordova.exec(null, null, "Hello", "hello", []);
+        }, false);
+
         console.log('Received Event: ' + id);
     }
 };

build ios

phonegap build ios

Open Xcode Window again and run application on simulator. You can see message 'Hello, this is a native function called from PhoneGap/Cordova!' in debug area console when You touch on screen,

Thank you.