목차

개요

전자정부 표준프레임워크 디바이스 API 실행환경은 추가 기능이 필요한 경우 써드파티 플러그인을 추가하거나 커스톰 플러그인을 추가로 개발하여 확장할수 있도록 되어 있다. Device API 웹소스는 아이폰,안드로이드에서 하나의 소스를 공유할수 있도록 구성할수 있어 중복개발공수를 줄일수 있다.

안드로이드 Device API

샘플 프로젝트 구조

관련소스

구분유형대상소스명비고
네이티브소스JAVAkr/go/egovframework/hyb/example/CustomPlugin.java 폰갭 코도바 안드로이드 Plugin 네이티브 소스파일
웹소스JS assets/www/custom.js 폰갭 코도바 Custom JavaScript
설정파일XMLres/xml/config.xml폰갭 코도바 설정파일

네이티브

CustomPlugin.java

execute method
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals(ACTION_ECHO)) {
            String message = args.getString(0);
            this.echo(message, callbackContext);
            return true;
        } else if (action.equals(ACTION_GET_MESSAGE)){
            this.getMessage(callbackContext);
        } else if (action.equals(ACTION_RUN_JAVASCRIPT_FUNCTION)){
            String functionName;
            if (args.length() == 0)
            	functionName = "args없음";
            else
            	functionName = args.getString(0);
            this.runJavaScriptFunction(functionName, callbackContext);
        }
        return false;
    }
webview의 javascript에서 호출 예제
    private void echo(String message, CallbackContext callbackContext) {
        if (message != null && message.length() > 0) {

            AlertDialog.Builder builder = new AlertDialog.Builder(this.cordova.getActivity());
            builder.setMessage(message).setPositiveButton("확인", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });

            AlertDialog dialog = builder.create();
            dialog.show();

            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, message));
            callbackContext.success(message);
        } else {
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "Expected one non-empty string argument."));
            callbackContext.error("Expected one non-empty string argument.");
        }
    }
webview의 javascript에서 호출에대한 결과전달 예제
    private void getMessage(CallbackContext callbackContext) throws JSONException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "Android native에서 생성된 메세지");

        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, jsonObject));
    }
네이티브에서 webview의 javascript 호출 예제
    private void runJavaScriptFunction(String functionName, final CallbackContext callbackContext) throws JSONException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "Android native에서 JavaScript 함수 호출, args="+functionName);

        final String javascriptString = "print_message(" + jsonObject.toString() + ")";

        Log.d(this.getClass().getSimpleName(), "=>>> print_message : " + javascriptString);
        
        this.webView.sendJavascript(javascriptString);

        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));

    }

웹소스

custom.js

echo method
function CustomMyPlugin(){}

CustomMyPlugin.prototype.echo = function(message){
    cordova.exec(null, null, "CustomMyPlugin", "echo", [message]);
}
getMessage method
CustomMyPlugin.prototype.getMessage = function(){
    var callbackSuccess = function(result){
        alert(result.name);
    };
    
    var callbackFail = function(error){
        alert(error);
    };
    
    cordova.exec(callbackSuccess, callbackFail, "CustomMyPlugin", "getMessage", []);
    
}
runJavaScript method
CustomMyPlugin.prototype.runJavaScript = function(){

	var callbackFail = function(error){
    	console.log(error);
        alert(error);
    };
    
    cordova.exec(null, callbackFail, "CustomMyPlugin", "runJavaScriptFunction", []);
    
}

function print_message(result){
	console.log("result : "+result);
    alert(result.name);
}

설정

config.xml
    <!-- eGovframe DeviceAPI Plug-In-->
    <feature name="EgovInterfacePlugin">
        <param name="android-package" value="kr.go.egovframework.hyb.plugin.EgovInterfacePlugin" />
    </feature>
    <feature name="StorageInfoPlugin">
        <param name="android-package" value="kr.go.egovframework.hyb.plugin.EgovStorageInfo" />
    </feature>
    <feature name="DeviceNumberPlugin">
        <param name="android-package" value="kr.go.egovframework.hyb.plugin.EgovDeviceNumber" />
    </feature>
    
    <feature name="CustomMyPlugin">
        <param name="android-package" value="kr.go.egovframework.hyb.plugin.CustomPlugin" />
    </feature>

아이폰 Device API

샘플 프로젝트 구조

관련소스

구분유형대상소스명비고
네이티브소스Objective-CeGovPlugins/CDVCustom.h 폰갭 코도바 아이폰 Plugin 네이티브 헤더파일
네이티브소스Objective-CeGovPlugins/CDVCustom.m 폰갭 코도바 아이폰 Plugin 네이티브 소스파일
웹소스JS Assets/www/custom.js 폰갭 코도바 Custom JavaScript
설정파일XMLAssets/config.xml폰갭 코도바 설정파일

네이티브

CDVCustom Class

header file
#import <Foundation/Foundation.h>
#import <Cordova/CDVPlugin.h>

@interface CDVCustom : CDVPlugin

- (void)echo:(CDVInvokedUrlCommand*)command;
- (void)getMessage:(CDVInvokedUrlCommand *)command;
- (void)runJavasScriptFuncion:(CDVInvokedUrlCommand *)command;

@end

webview의 javascript에서 호출 예제
- (void)echo:(CDVInvokedUrlCommand*)command
{
    NSString *message = [command.arguments objectAtIndex:0];
    [[[UIAlertView alloc] initWithTitle:@"iOS eGovframe" message:message delegate:nil cancelButtonTitle:@"취소" otherButtonTitles:@"확인", nil] show];
}
webview의 javascript에서 호출에대한 결과전달 예제
- (void)runJavasScriptFuncion:(CDVInvokedUrlCommand *)command
{
    NSDictionary *jsonInfo = @{@"name": @"iOS native에서 자바스크립트 함수 호출"};
    
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonInfo options:NSJSONWritingPrettyPrinted error:&error];
    CDVPluginResult *pluginResult = nil;
    
    if (!error) {
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSString *javaScriptString = [NSString stringWithFormat:@"print_message(%@)", jsonString];
        
        [self.webView stringByEvaluatingJavaScriptFromString:javaScriptString];
        
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    } else {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
    }
    
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    
}
네이티브에서 webview의 javascript 호출 예제
    private void runJavaScriptFunction(String functionName, final CallbackContext callbackContext) throws JSONException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "Android native에서 JavaScript 함수 호출, args="+functionName);

        final String javascriptString = "print_message(" + jsonObject.toString() + ")";

        Log.d(this.getClass().getSimpleName(), "=>>> print_message : " + javascriptString);
        
        this.webView.sendJavascript(javascriptString);

        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));

    }

웹소스

custom.js

echo method
function CustomMyPlugin(){}

CustomMyPlugin.prototype.echo = function(message){
    cordova.exec(null, null, "CustomMyPlugin", "echo", [message]);
}
getMessage method
CustomMyPlugin.prototype.getMessage = function(){
    var callbackSuccess = function(result){
        alert(result.name);
    };
    
    var callbackFail = function(error){
        alert(error);
    };
    
    cordova.exec(callbackSuccess, callbackFail, "CustomMyPlugin", "getMessage", []);
    
}
runJavaScript method
CustomMyPlugin.prototype.runJavaScript = function(){

	var callbackFail = function(error){
    	console.log(error);
        alert(error);
    };
    
    cordova.exec(null, callbackFail, "CustomMyPlugin", "runJavaScriptFunction", []);
    
}

function print_message(result){
	console.log("result : "+result);
    alert(result.name);
}

설정

config.xml
    <!-- eGovframe DeviceAPI Plug-In-->
    <feature name="StorageInfoAPI">
        <param name="ios-package" value="EgovStorageInfo"/>
    </feature>
    <feature name="InterfaceAPI">
        <param name="ios-package" value="EgovInterface"/>
    </feature>
    
    <feature name="CustomMyPlugin">
        <param name="ios-package" value="CDVCustom" />
    </feature>