Skip to content

Instantly share code, notes, and snippets.

@ainvyu
Created October 14, 2013 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ainvyu/6978354 to your computer and use it in GitHub Desktop.
Save ainvyu/6978354 to your computer and use it in GitHub Desktop.
plugin

config.xml에 추가

phonegap document 상에는 config.xml 에 다음과 같은 식으로 추가하면 된다고 하나 저대로 하면 안된다.

<plugin name=PluginName value="org.apache.cordova.plugin.PluginClassName>

실제 코드(org/apache/cordova/PluginManager.java의 loadPlugins 메소드)를 보면 plugin element의 service명을 얻어와야 하는데 코드 상에 걸러주는 부분이 없다. 때문에 feature를 써야한다.

그래서 다음과 같이 추가하면 된다.

<feature name="PluginName">
    <param name="android-package" value="org.apache.cordova.plugin.PluginClassName" />
</feature>

실제로 PluginManager.java의 ff27ad332b3bd6cc0a17f5bb49cf239b4daa43d1 커밋에 보면

Log.d(TAG, "<plugin> tags are deprecated, please use <features> instead. <plugin> will no longer work as of Cordova 3.0");

이란 코드가 있었는지 지워진거 보면 plugin 태그는 없앨 예정이 맞으며 위와 같이 하는게 맞는 것으로 보인다.

Share 예제

Share.java를 src/org/apache/cordova/plugin 디렉토리에 추가하고 다음 내용을 넣는다.

/**
 * Created by envi on 13. 7. 24.
 */
package org.apache.cordova.plugin;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.util.Log;

public class Share extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
        try {
            Log.d("PhoneGapLog", "Share::execute");
            JSONObject jo = args.getJSONObject(0);

            doSendIntent(jo.getString("subject"), jo.getString("text"));

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

            return true;
        } catch (JSONException e) {
            Log.d("PhoneGapLog", "Share Plugin: Error: " + PluginResult.Status.JSON_EXCEPTION);
            e.printStackTrace();

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

            return false;
        }
    }

    private void doSendIntent(String subject, String text) {
        Log.d("PhoneGapLog", "Call doSendIntent");
        Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
        sendIntent.setType("text/plain");
        sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
        sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);

        this.cordova.startActivityForResult(this, sendIntent, 0);
    }
}

share.js를 assets/www/js 디렉토리에 만들고 다음 내용을 넣는다.

var Share = function() {};

Share.prototype.show = function(content, success, fail) {
    return cordova.exec( function(args) {
            success(args);
        },
        function(args) {
            fail(args);
        }, "Share", 'some_action', [ content ]
    );
};

if (!window.plugins) {
    window.plugins = {};
}

if (!window.plugins.share) {
    window.plugins.share = new Share();
}

index.html에 다음을 넣는다.

<script type="text/javascript" src="js/share.js"></script>

...

<script type="text/javascript">
    app.initialize();

    document.addEventListener("deviceready", function() {
        show();
    }, false);

    $(document).ready( function () {
    });

    function show() {
        window.plugins.share.show({
                subject: 'I like turtles',
                text: 'http://www.mndaily.com'
            },
            function() {
                alert('Share Success');
            }, // Success function
            function() {
                alert('Share failed');
            } // Failure function
        );
    }
</script>

res/xml/config.xml에 다음 내용을 widget 노드의 자식으로 넣는다.

<feature name="Share">
    <param name="android-package" value="org.apache.cordova.plugin.Share" />
</feature>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment