A social sharing library for Android.
Actively maintained.
- Uses Builder pattern
- Provides a convenient interface to be used in Unity as a native plugin or JNI
- Supports Sharesheet/Chooser
- Supports basic sharing
- Supports HTML text and literal text (prefilled text, some social platforms do not support it)
- Supports binary content (such as images)
- Supports custom mime type
- Aimed for JNI and native bridge usages
- Available on Maven Central, Jitpack and GitHub packages
- Uses
ShareCompat.IntentBuilder
- Provides custom methods for Unity
Artifact is available on Maven Central, Jitpack and GitHub packages.
io.bayat.android:social-sharing:1.1.0
io.bayat.android:social-sharing-unity:1.1.0
- Add as a dependency:
dependencies {
implementation "io.bayat.android:social-sharing:1.1.0"
... // Other dependencies
}
-
Add the dependency to your dependencies xml file to your Unity project:
<dependencies> <androidPackages> <androidPackage spec="io.bayat.android:social-sharing-unity:1.1.0" /> </androidPackages> </dependencies>
-
Use it through Android JNI
Use the SocialSharing.Builder
to create a SocialSharing
instance:
String mimeType = "text/plain";
String message = "Hello World";
SocialSharing sharing = new SocialSharing.Builder(myActivity)
.setText(message)
.setType(mimeType)
.build();
- Sharesheet: Call
setUseSharesheet(true)
to use Android's Sharesheet:
String mimeType = "image/jpeg";
String message = "Hello World";
SocialSharing sharing = new SocialSharing.Builder(myActivity)
.setText(message)
.setStream(Uri.fromFile(new File("/sdcard/cats.jpg"))
.setType(mimeType)
.build();
// Call send method
sharing.send(myActivity);
- Basic: Call
setUseSharesheet(false)
to use basic sharing:
String mimeType = "image/jpeg";
String message = "Hello World";
SocialSharing sharing = new SocialSharing.Builder(myActivity)
.setText(message)
.setStream(Uri.fromFile(new File("/sdcard/cats.jpg"))
.setType(mimeType)
.build();
// Call send method
sharing.send(myActivity);
The SocialSharingUnity
extends from SocialSharing
so it extends the Builder
too, the only differences are the URI and the send
method:
String mimeType = "image/jpeg";
String message = "Hello World";
SocialSharingUnity sharing = new SocialSharingUnity.Builder()
.setText(message)
.setUnityStream("myscreenshot.jpg")
.setType(mimeType)
.build();
// Call send method, the activity parameter is not required as the SocialSharingUnity will use the current activity of UnityPlayer
sharing.send();
Also for JNI use cases, use this class and the methods of setUnityStream
and addUnityStream
for specifying files and content to be shared instead of setUri
or addUri
:
const string BuilderClassName = "io.bayat.android.social.sharing.unity.SocialSharingUnity$UnityBuilder";
bool useSharesheet = true;
bool useRichPreview = true;
string text = "Hello World from Unity!";
string title = "My Custom Chooser Title";
string imagePath = Application.persistentDataPath + "/screenshot.jpg";
string mimeType = "image/jpg";
using (AndroidJavaObject builder = new AndroidJavaObject(BuilderClassName))
{
builder.Call<AndroidJavaObject>("setText", text);
builder.Call<AndroidJavaObject>("setChooserTitle", title);
builder.Call<AndroidJavaObject>("setUnityStream", imagePath);
builder.Call<AndroidJavaObject>("setType", data.MimeType);
builder.Call("startChooser");
}
send(Activity activity)
: Send the content to be shared using the given Activity
setText(String text)
: Set the text to be sharedsetHtmlText(String htmlText)
: Set the HTML text to be shared (the HTML text is parsed before sharing, useful for formatting)setStream(Uri uri)
: Set the URI of the content to be sharedaddStream(Uri uri)
: Add a URI to the content to be sharedsetChoosterTitle(String title)
: Set the sharesheet/chooser titlesetType(String type)
: Set the mime type of the content to be sharedbuild()
: Build theSocialSharing
instancestartChooser
: Start the sharesheet/chooser activity
send()
: Send the content to be shared (uses UnityPlayer.currentActivity as the Activity)
Extends from SocialSharing.Builder
and adds the following methods:
setUnityStream(String path)
: Set the URI of the content to be shared from a Unity (resolves the file path to a content URI in Android)addUnityStream(String path)
: Add a URI to the content to be shared from a Unity (resolves the file path to a content URI in Android)
The setText
method sets a prefilled text to be shared by the user, but some social platforms do not support it, mainly Facebook amd all of their other various social services including Instagram, Messenger, WhatsApp and Facebook Lite, but other platforms such as Twitter, Discord, Slack and others support it. (this won't have any issues on your app, it will just ignore the text and share the content without it)
Learn more on Meta Platform Terms and Platform Policy 2.3 Example and Explanation
Check the CHANGELOG.md file for more information.
- Android - Send simple data to other apps
- Call Java and Kotlin plug-in code from C# scripts
- NicholasSheehan/Unity-Native-Sharing
Check the LICENSE file for more information.
- Developed and maintained by Hasan Bayat
Made with ❤️ by Bayat