Commit 49791a9e authored by Thierry BUGEAT's avatar Thierry BUGEAT

#46 Add Cordova plugin social sharing

parent a0097b38
......@@ -21,6 +21,9 @@
<allow-intent href="geo:*" />
<platform name="android">
<allow-intent href="market:*" />
<feature name="SocialSharing">
<param name="android-package" value="nl.xservices.plugins.SocialSharing" />
</feature>
</platform>
<platform name="ios">
<allow-intent href="itms:*" />
......
......@@ -24,6 +24,9 @@
},
"cordova-plugin-inappbrowser": {
"PACKAGE_NAME": "com.bugeat.myfeeds"
},
"cordova-plugin-x-socialsharing": {
"PACKAGE_NAME": "com.bugeat.myfeeds"
}
},
"dependent_plugins": {}
......
This diff is collapsed.
{
"name": "cordova-plugin-x-socialsharing",
"version": "5.0.10",
"description": "Share text, images (and other files), or a link via the native sharing widget of your device. Android is fully supported, as well as iOS 6 and up. WP8 has somewhat limited support.",
"cordova": {
"id": "cordova-plugin-x-socialsharing",
"platforms": [
"ios",
"android",
"wp8"
]
},
"repository": {
"type": "git",
"url": "git+https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin.git"
},
"keywords": [
"Social",
"Share",
"Twitter",
"Facebook",
"Email",
"SMS",
"WhatsApp",
"Tumblr",
"Pocket",
"LinkedIn",
"cordova",
"ecosystem:cordova",
"cordova-ios",
"cordova-android",
"cordova-windows"
],
"engines": [
{
"name": "cordova",
"version": ">=3.0.0"
}
],
"author": "Eddy Verbruggen - @EddyVerbruggen",
"license": "MIT",
"bugs": {
"url": "https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin/issues"
},
"homepage": "https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin#readme"
}
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-x-socialsharing"
version="5.0.10">
<name>SocialSharing</name>
<description>
Share text, images (and other files), or a link via the native sharing widget of your device.
Android is fully supported, as well as iOS 6 and up. WP8 has somewhat limited support.
</description>
<author>Eddy Verbruggen</author>
<license>MIT</license>
<keywords>Social, Share, Twitter, Facebook, Email, SMS, WhatsApp, Tumblr, Pocket, LinkedIn</keywords>
<repo>https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin.git</repo>
<issue>https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin/issues</issue>
<engines>
<engine name="cordova" version=">=3.0.0"/>
</engines>
<js-module src="www/SocialSharing.js" name="SocialSharing">
<clobbers target="window.plugins.socialsharing" />
</js-module>
<!-- ios -->
<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="SocialSharing">
<param name="ios-package" value="SocialSharing"/>
<param name="onload" value="true" />
</feature>
</config-file>
<header-file src="src/ios/SocialSharing.h"/>
<source-file src="src/ios/SocialSharing.m"/>
<framework src="Social.framework" weak="true" />
<framework src="MessageUI.framework" weak="true" />
</platform>
<!-- android -->
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="SocialSharing">
<param name="android-package" value="nl.xservices.plugins.SocialSharing" />
</feature>
</config-file>
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</config-file>
<source-file src="src/android/nl/xservices/plugins/SocialSharing.java" target-dir="src/nl/xservices/plugins"/>
</platform>
<!-- wp8 -->
<platform name="wp8">
<config-file target="config.xml" parent="/*">
<feature name="SocialSharing">
<param name="wp-package" value="SocialSharing"/>
</feature>
</config-file>
<source-file src="src/wp8/SocialSharing.cs" />
<framework src="src/wp8/Newtonsoft.Json.dll" custom="true"/>
</platform>
<!-- windows -->
<platform name="windows">
<js-module src="src/windows/SocialSharingProxy.js" name="SocialSharingProxy">
<merges target="" />
</js-module>
<source-file src="" />
</platform>
</plugin>
#import <Cordova/CDV.h>
#import <MessageUI/MFMailComposeViewController.h>
@interface SocialSharing : CDVPlugin <UIPopoverControllerDelegate, MFMailComposeViewControllerDelegate, UIDocumentInteractionControllerDelegate>
@property (nonatomic, strong) MFMailComposeViewController *globalMailComposer;
@property (nonatomic, strong) UIDocumentInteractionController * documentInteractionController;
@property (retain) NSString * tempStoredFile;
@property (retain) CDVInvokedUrlCommand * command;
- (void)available:(CDVInvokedUrlCommand*)command;
- (void)setIPadPopupCoordinates:(CDVInvokedUrlCommand*)command;
- (void)share:(CDVInvokedUrlCommand*)command;
- (void)canShareVia:(CDVInvokedUrlCommand*)command;
- (void)canShareViaEmail:(CDVInvokedUrlCommand*)command;
- (void)shareVia:(CDVInvokedUrlCommand*)command;
- (void)shareViaTwitter:(CDVInvokedUrlCommand*)command;
- (void)shareViaFacebook:(CDVInvokedUrlCommand*)command;
- (void)shareViaFacebookWithPasteMessageHint:(CDVInvokedUrlCommand*)command;
- (void)shareViaWhatsApp:(CDVInvokedUrlCommand*)command;
- (void)shareViaSMS:(CDVInvokedUrlCommand*)command;
- (void)shareViaEmail:(CDVInvokedUrlCommand*)command;
- (void)shareViaInstagram:(CDVInvokedUrlCommand*)command;
- (void)saveToPhotoAlbum:(CDVInvokedUrlCommand*)command;
@end
This diff is collapsed.
var cordova = require('cordova');
module.exports = {
share: function (win, fail, args) {
//Text Message
var message = args[0];
//Title
var subject = args[1];
//File(s) Path
var fileOrFileArray = args[2];
//Web link
var url = args[3];
var folder = Windows.Storage.ApplicationData.current.temporaryFolder;
var getExtension = function (strBase64) {
return strBase64.substring(strBase64.indexOf("/") + 1, strBase64.indexOf(";base64"));
};
var replaceAll = function (str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
};
var sanitizeFilename = function (name) {
return replaceAll(name, "[:\\\\/*?|<> ]", "_");
};
var getFileName = function (position, fileExtension) {
var fileName = (subject ? sanitizeFilename(subject) : "file") + (position == 0 ? "" : "_" + position) + "." + fileExtension;
return fileName;
};
var createTemporalFile = function (fileName, buffer) {
var filePath = "";
return folder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
filePath = file.path;
return Windows.Storage.FileIO.writeBufferAsync(file, buffer);
}).then(function(){
return Windows.Storage.StorageFile.getFileFromPathAsync(filePath);
});
};
var doShare = function (e) {
e.request.data.properties.title = subject?subject: "Sharing";
if (message) e.request.data.setText(message);
if (url) e.request.data.setWebLink(new Windows.Foundation.Uri(url));
if (fileOrFileArray.length > 0) {
var deferral = e.request.getDeferral();
var storageItems = [];
var filesCount = fileOrFileArray.length;
var completeFile = function () {
if (!--filesCount) {
storageItems.length && e.request.data.setStorageItems(storageItems);
deferral.complete();
}
};
for (var i = 0; i < fileOrFileArray.length; i++) {
var file = fileOrFileArray[i];
if (file.indexOf("data:") >= 0) {
var fileName = getFileName(i, getExtension(file));
var buffer = Windows.Security.Cryptography.CryptographicBuffer.decodeFromBase64String(file.split(',')[1]);
if (buffer) {
createTemporalFile(fileName, buffer).done(
function (file) {
storageItems.push(file);
completeFile();
},
function () {
completeFile();
}
);
}
else {
completeFile();
}
}
else {
Windows.Storage.StorageFile.getFileFromPathAsync(file).done(
function (file) {
storageItems.push(file);
completeFile();
},
function () {
completeFile();
}
);
}
}
}
};
var dataTransferManager = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView();
dataTransferManager.addEventListener("datarequested", doShare);
try {
Windows.ApplicationModel.DataTransfer.DataTransferManager.showShareUI();
win(true);
} catch (err) {
fail(err);
}
},
canShareViaEmail: function (win, fail, args) {
win(true);
},
shareViaEmail: function (win, fail, args) {
//Text Message
var message = args[0];
//Title
var subject = args[1];
//File(s) Path
var fileOrFileArray = args[5];
var doShare = function (e) {
e.request.data.properties.title = subject ? subject : "Sharing";
if (message) {
var htmlFormat = Windows.ApplicationModel.DataTransfer.HtmlFormatHelper.createHtmlFormat(message);
e.request.data.setHtmlFormat(htmlFormat);
}
if (fileOrFileArray.length > 0) {
var deferral = e.request.getDeferral();
var storageItems = [];
var filesCount = fileOrFileArray.length;
for (var i = 0; i < fileOrFileArray.length; i++) {
Windows.Storage.StorageFile.getFileFromPathAsync(fileOrFileArray[i]).done(
function (index, file) {
var path = fileOrFileArray[index];
var streamRef = Windows.Storage.Streams.RandomAccessStreamReference.createFromFile(file);
e.request.data.resourceMap[path] = streamRef;
storageItems.push(file);
if (!--filesCount) {
e.request.data.setStorageItems(storageItems);
deferral.complete();
}
}.bind(this, i),
function () {
if (!--filesCount) {
e.request.data.setStorageItems(storageItems);
deferral.complete();
}
}
);
}
}
};
var dataTransferManager = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView();
dataTransferManager.addEventListener("datarequested", doShare);
try {
Windows.ApplicationModel.DataTransfer.DataTransferManager.showShareUI();
win(true);
} catch (err) {
fail(err);
}
}
};
require("cordova/exec/proxy").add("SocialSharing", module.exports);
\ No newline at end of file
using Microsoft.Phone.Tasks;
using WPCordovaClassLib.Cordova;
using WPCordovaClassLib.Cordova.Commands;
using WPCordovaClassLib.Cordova.JSON;
using Newtonsoft.Json;
namespace Cordova.Extension.Commands
{
public class SocialSharing : BaseCommand
{
public void available(string jsonArgs)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
}
public void share(string jsonArgs)
{
var options = JsonHelper.Deserialize<string[]>(jsonArgs);
var message = options[0];
var title = options[1];
var files = JsonHelper.Deserialize<string[]>(options[2]);
var link = options[3];
if (!"null".Equals(link))
{
ShareLinkTask shareLinkTask = new ShareLinkTask();
shareLinkTask.Title = title;
shareLinkTask.LinkUri = new System.Uri(link, System.UriKind.Absolute);
shareLinkTask.Message = message;
shareLinkTask.Show();
}
else if (files != null && files.Length > 0)
{
ShareLinkTask shareLinkTask = new ShareLinkTask();
shareLinkTask.Title = title;
shareLinkTask.LinkUri = new System.Uri(files[0], System.UriKind.Absolute);
shareLinkTask.Message = message;
shareLinkTask.Show();
}
else
{
var shareStatusTask = new ShareStatusTask { Status = message };
shareStatusTask.Show();
}
// unfortunately, there is no way to tell if something was shared, so just invoke the successCallback
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
}
public void canShareViaEmail(string jsonArgs)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
}
// HTML and attachments are currently not supported on WP8
public void shareViaEmail(string jsonArgs)
{
var options = JsonHelper.Deserialize<string[]>(jsonArgs);
EmailComposeTask draft = new EmailComposeTask();
draft.Body = options[0];
draft.Subject = options[1];
if (!"null".Equals(options[2]))
{
draft.To = string.Join(",", options[2]);
}
if (!"null".Equals(options[3]))
{
draft.Cc = string.Join(",", options[3]);
}
if (!"null".Equals(options[4]))
{
draft.Bcc = string.Join(",", options[4]);
}
draft.Show();
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, true));
}
public void shareViaSMS(string jsonArgs)
{
var options = JsonHelper.Deserialize<string[]>(jsonArgs);
SmsComposeTask smsComposeTask = new SmsComposeTask();
smsComposeTask.To = options[1];
SMSMessageClass m = JsonConvert.DeserializeObject<SMSMessageClass>(options[0]);
smsComposeTask.Body = m.message;
smsComposeTask.Show();
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, true));
}
}
public class SMSMessageClass
{
public string message { get; set; }
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:rim="http://www.blackberry.com/ns/widgets"
xmlns:android="http://schemas.android.com/apk/res/android"
id="nl.x-services.plugins.socialsharing.tests"
version="4.3.15">
<name>SocialSharing Tests</name>
<author>Nicolas Oliver</author>
<license>MIT</license>
<js-module src="test.js" name="tests"></js-module>
</plugin>
\ No newline at end of file
This diff is collapsed.
var cordova = require('cordova');
function SocialSharing() {
}
// Override this method (after deviceready) to set the location where you want the iPad popup arrow to appear.
// If not overridden with different values, the popup is not used. Example:
//
// window.plugins.socialsharing.iPadPopupCoordinates = function() {
// return "100,100,200,300";
// };
SocialSharing.prototype.iPadPopupCoordinates = function () {
// left,top,width,height
return "-1,-1,-1,-1";
};
SocialSharing.prototype.setIPadPopupCoordinates = function (coords) {
// left,top,width,height
cordova.exec(function() {}, this._getErrorCallback(function() {}, "setIPadPopupCoordinates"), "SocialSharing", "setIPadPopupCoordinates", [coords]);
};
SocialSharing.prototype.available = function (callback) {
cordova.exec(function (avail) {
callback(avail ? true : false);
}, null, "SocialSharing", "available", []);
};
SocialSharing.prototype.share = function (message, subject, fileOrFileArray, url, successCallback, errorCallback) {
cordova.exec(successCallback, this._getErrorCallback(errorCallback, "share"), "SocialSharing", "share", [message, subject, this._asArray(fileOrFileArray), url]);
};
SocialSharing.prototype.shareViaTwitter = function (message, file /* multiple not allowed by twitter */, url, successCallback, errorCallback) {
var fileArray = this._asArray(file);
var ecb = this._getErrorCallback(errorCallback, "shareViaTwitter");
if (fileArray.length > 1) {
ecb("shareViaTwitter supports max one file");
} else {
cordova.exec(successCallback, ecb, "SocialSharing", "shareViaTwitter", [message, null, fileArray, url]);
}
};
SocialSharing.prototype.shareViaFacebook = function (message, fileOrFileArray, url, successCallback, errorCallback) {
cordova.exec(successCallback, this._getErrorCallback(errorCallback, "shareViaFacebook"), "SocialSharing", "shareViaFacebook", [message, null, this._asArray(fileOrFileArray), url]);
};
SocialSharing.prototype.shareViaFacebookWithPasteMessageHint = function (message, fileOrFileArray, url, pasteMessageHint, successCallback, errorCallback) {
pasteMessageHint = pasteMessageHint || "If you like you can paste a message from your clipboard";
cordova.exec(successCallback, this._getErrorCallback(errorCallback, "shareViaFacebookWithPasteMessageHint"), "SocialSharing", "shareViaFacebookWithPasteMessageHint", [message, null, this._asArray(fileOrFileArray), url, pasteMessageHint]);
};
SocialSharing.prototype.shareViaWhatsApp = function (message, fileOrFileArray, url, successCallback, errorCallback) {
cordova.exec(successCallback, this._getErrorCallback(errorCallback, "shareViaWhatsApp"), "SocialSharing", "shareViaWhatsApp", [message, null, this._asArray(fileOrFileArray), url, null]);
};
SocialSharing.prototype.shareViaWhatsAppToReceiver = function (receiver, message, fileOrFileArray, url, successCallback, errorCallback) {
cordova.exec(successCallback, this._getErrorCallback(errorCallback, "shareViaWhatsAppToReceiver"), "SocialSharing", "shareViaWhatsApp", [message, null, this._asArray(fileOrFileArray), url, receiver]);
};
SocialSharing.prototype.shareViaSMS = function (options, phonenumbers, successCallback, errorCallback) {
var opts = options;
if (typeof options == "string") {
opts = {"message":options}; // for backward compatibility as the options param used to be the message
}
cordova.exec(successCallback, this._getErrorCallback(errorCallback, "shareViaSMS"), "SocialSharing", "shareViaSMS", [opts, phonenumbers]);
};
SocialSharing.prototype.shareViaEmail = function (message, subject, toArray, ccArray, bccArray, fileOrFileArray, successCallback, errorCallback) {
cordova.exec(successCallback, this._getErrorCallback(errorCallback, "shareViaEmail"), "SocialSharing", "shareViaEmail", [message, subject, this._asArray(toArray), this._asArray(ccArray), this._asArray(bccArray), this._asArray(fileOrFileArray)]);
};
SocialSharing.prototype.canShareVia = function (via, message, subject, fileOrFileArray, url, successCallback, errorCallback) {
cordova.exec(successCallback, this._getErrorCallback(errorCallback, "canShareVia"), "SocialSharing", "canShareVia", [message, subject, this._asArray(fileOrFileArray), url, via]);
};
SocialSharing.prototype.canShareViaEmail = function (successCallback, errorCallback) {
cordova.exec(successCallback, this._getErrorCallback(errorCallback, "canShareViaEmail"), "SocialSharing", "canShareViaEmail", []);
};
SocialSharing.prototype.shareViaInstagram = function (message, fileOrFileArray, successCallback, errorCallback) {
cordova.exec(successCallback, this._getErrorCallback(errorCallback, "shareViaInstagram"), "SocialSharing", "shareViaInstagram", [message, null, this._asArray(fileOrFileArray), null]);
};
SocialSharing.prototype.shareVia = function (via, message, subject, fileOrFileArray, url, successCallback, errorCallback) {
cordova.exec(successCallback, this._getErrorCallback(errorCallback, "shareVia"), "SocialSharing", "shareVia", [message, subject, this._asArray(fileOrFileArray), url, via]);
};
SocialSharing.prototype.saveToPhotoAlbum = function (fileOrFileArray, successCallback, errorCallback) {
cordova.exec(successCallback, this._getErrorCallback(errorCallback, "saveToPhotoAlbum"), "SocialSharing", "saveToPhotoAlbum", [this._asArray(fileOrFileArray)]);
};
SocialSharing.prototype._asArray = function (param) {
if (param == null) {
param = [];
} else if (typeof param === 'string') {
param = new Array(param);
}
return param;
};
SocialSharing.prototype._getErrorCallback = function (ecb, functionName) {
if (typeof ecb === 'function') {
return ecb;
} else {
return function (result) {
console.log("The injected error callback of '" + functionName + "' received: " + JSON.stringify(result));
}
}
};
SocialSharing.install = function () {
if (!window.plugins) {
window.plugins = {};
}
window.plugins.socialsharing = new SocialSharing();
return window.plugins.socialsharing;
};
cordova.addConstructor(SocialSharing.install);
......@@ -46,5 +46,13 @@
},
"is_top_level": true,
"variables": {}
},
"cordova-plugin-x-socialsharing": {
"source": {
"type": "registry",
"id": "cordova-plugin-x-socialsharing"
},
"is_top_level": true,
"variables": {}
}
}
\ No newline at end of file
......@@ -24,6 +24,9 @@
},
"cordova-plugin-inappbrowser": {
"PACKAGE_NAME": "com.bugeat.myfeeds"
},
"cordova-plugin-x-socialsharing": {
"PACKAGE_NAME": "com.bugeat.myfeeds"
}
},
"dependent_plugins": {}
......
......@@ -39,6 +39,9 @@
},
"cordova-plugin-whitelist": {
"PACKAGE_NAME": "com.bugeat.myfeeds"
},
"cordova-plugin-x-socialsharing": {
"PACKAGE_NAME": "com.bugeat.myfeeds"
}
},
"dependent_plugins": {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment