Phonegap - 将分享功能拓展至电子邮件、Twitter和Facebook

14

有没有示例如何使用Phonegap框架编写共享URL到电子邮件、Twitter和Facebook的功能?例如,在Android中,这个功能在90%的应用程序中都有。在iPhone中,它在任何应用程序中都有。在techcrunch的iPhone应用程序中,您可以看到它,当您打开一篇文章时。能否也用Phonegap创建此功能?


你最终解决了这个问题吗?寻找适用于iOS/PhoneGap(Cordova 1.7)的简单解决方案。 - nate8684
3个回答

7
你可以使用以下插件代码在Android上完成此操作。我还没有在其他地方发布这个,但最终我希望将其添加到phonegap插件存储库中作为Android的插件。
JAVASCRIPT:
var Share = function() {};

Share.prototype.show = function(content) {
    return PhoneGap.exec(
    function(args) {
        console.log("phonegap share plugin - success!")
    }, function(args) {
        console.log("phonegap share plugin - failed")
    }, 'Share', '', content);
};

PhoneGap.addConstructor(function() {
    PhoneGap.addPlugin('share', new Share());
    PluginManager.addService("Share","com.COMPANYNAME(CHANGEME).android.plugins.Share");
});

JAVA在安卓中的应用:

package com.COMPANYNAME(CHANGEME).android.plugins;

import org.json.JSONArray;
import org.json.JSONException;
import android.content.Intent;

import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;

public class Share extends Plugin {
    private String callback;

    @Override
    public PluginResult execute(String action, JSONArray args, String callbackId) {
        PluginResult mPlugin = null;
        try {
            mPlugin = activateSharing(args.getString(0), args.getString(1));
        } catch (JSONException e) {
            Log.e("JSON Exception", e.toString());
        }
        mPlugin.setKeepCallback(true);
        this.callback = callbackId;
        return mPlugin;
    }

    private PluginResult activateSharing(String title, String body) {
        final Intent shareIntent = new Intent(
        android.content.Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
        shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);

        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ctx.startActivity(Intent.createChooser(shareIntent, "Share"));
        return new PluginResult(PluginResult.Status.OK);
    }
}

5

近三年之后,这里有一个插件可以使用相同的API在Android和iOS上进行分享。 https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin

此插件也可在PhoneGap Build上使用!

示例:

window.plugins.socialsharing.share('Google is awesome, WOOT!', 'Google facts', 'https://www.google.com/images/srpr/logo11w.png', 'http://www.google.com');

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接