Mono46
12/13/2013 - 7:30 AM

PhoneGapプラグインの作成 ref: http://qiita.com/Mono/items/35d9672a9a61b5ce60b5

PhoneGapプラグインの作成 ref: http://qiita.com/Mono/items/35d9672a9a61b5ce60b5

window.plugin = {
    hoge: {
        fuga : function() {
            cordova.exec(function(message) {
                    // success callback
                },
                function(message) {
                    // error callback
                },
                'hoge', 'fuga', [args]);
    }
};
#import “Cordova/CDV.h”
#import “hoge.h”

@implemention hoge

- (void) fuga : (CDVInvokeUrlCommand*) command {

    // UIViewController取得
    UIViewController *uiViewController = super.viewController;

    // UIWebView取得
    UIWebView webView = super.webView;

    // JavaScript側からの引数取得
    String arg = [command argumentAtIndex: 0];

    // JavaScriptのコールバックへデータを送る
    // 成功の場合
    CDVPluginResult plutinResult =[CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString: @“成功だよ!”];
    // エラーの場合
    CDVPluginResult plutinResult =[CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString: @“エラー発生!”];

    // データ送信
    [super.commandDelegate sendPluginResult: pluginResult callbackId: command.callbackId];
}

@end
package com.plugin.pack;

import apache.cordova.CordovaPlugin;
import apache.cordova.CallbackContext;
import apache.cordova.PluginResullt;

public class hoge extends CordovaPlugin {
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
        if (action.equals(“hoge”)) {
            this.hoge();

            // 引数データの取得
            String param = args.getString(0);

            // Javascriptのコールバックへデータを送る。
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, “成功したよ!”);
            // エラーの場合
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.Error, “エラー発生!”);
            return true;
        }
        return false;
    }
    private void hoge() {
        // いろいろと処理
        // 表示しているActivityの取得
        Activity activity = super.cordova.getActivity(); // 取得したらあとはお好きにどうぞ。
        //WebView 取得
        WebView webView = super.webView; //あとは好きに(ry
    }
}
#import <Cordova/CDVPlugin.h>
#import “Foundation/Foundation.h”

@interface hoge : CDVPlugin

- (void) fuga : (CDVInvokeUrlCommand*) command;

@end
<widget id=“”com.fuga.hoge” version=”2.0.0” xmlns=“http://www.w3org/ns/widgets”>
<!— ----------中略---------- —>
    <feature name=“hoge”>
        <param name=“ios-package” value=”hoge”>
    </feature>
<!— ----------中略---------- —>
</widget>