Native Share for Android & iOS [Open Source]

NOTICE: This asset is no longer maintained! Consider using alternative sharing solutions that are actively being maintained. While using this package, expect issues when sharing a file/image/video with a text (see FAQ).

Hello there,

Native Share, as the name implies, is a plugin to natively share file(s) and/or text from within your app on Android and iOS. It supports sharing multiple files and adding subject/text to the shared content. It uses a ContentProvider on Android to support the latest versions of Android, as well.

Asset Store: Native Share for Android & iOS | Integration | Unity Asset Store
Also available at: GitHub - yasirkula/UnityNativeShare: A Unity plugin to natively share files (images, videos, documents, etc.) and/or plain text on Android & iOS
Discord: yasirkula Unity Assets
Support the developer :coffee:

FAQ

  • Can I share on a specific app?
  • I can’t share image with text on X app

It is just not possible to share an image/file with text/subject on some apps (e.g. Facebook), they intentionally omit either the image or the text from the shared content. These apps require you to use their own SDKs for complex share actions. For best compatibility, I’d recommend you to share either only image or only text.

  • I can’t build the project to Android, it says “Android resource linking failed: unexpected element found in ” in the error message

NativeShare adds <queries> element to AndroidManifest.xml due to the new package visibility change. The build error can be fixed by following these steps: ŰŻŰčم Ű­ŰČÙ…Ű© ARCore Ù„Ù…Ű·ÙˆÙ‘Ű±ÙŠ Unity  |  Google for Developers (in my tests, changing “Gradle installed with Unity” wasn’t necessary). In the worst case, if you are OK with NativeShare not working on some of the affected devices, then you can open NativeShare.aar with WinRAR or 7-Zip and then remove the <queries>...</queries> element from AndroidManifest.xml.

  • When using Unity as a Library on Android, the app crashes when sharing a file

If you’re running the Unity activity in a separate process, then modify AndroidManifest.xml inside NativeShare.aar so that both NativeShareCustomShareDialogActivity and NativeShareBroadcastListener also run on the same process, e.g:

<activity android:name=".NativeShareCustomShareDialogActivity" ... android:process=":YourProcess" />
<receiver android:name=".NativeShareBroadcastListener" ... android:process=":YourProcess" />
  • Can’t share, it says “java.lang.ClassNotFoundException: com.yasirkula.unity.NativeShare” in Logcat

If you are sure that your plugin is up-to-date, then enable Custom Proguard File option from Player Settings and add the following line to that file: -keep class com.yasirkula.unity.* { *; }

Enjoy!

16 Likes

Now live on Asset Store!

2 Likes

I love it! Works perfectly and very simple to use!

I have a question: how can I share a simple URL to a news article? (no photo or video)

Simply using new NativeShare().SetText( “http://google.com” ).Share(); should be enough.

1 Like

Works great!

1 Like

Hi,
I always get the log as
“Can’t file ContentProvider, share not possible!”
And I have Uploaded my manifest file.
Kindly help me with this.

3446959–272985–AndroidManifest_Txt.txt (1.37 KB)

1 Like

That’s weird; it works fine for me while using your AndroidManifest. May I ask your device model and Android version? Also, can you send the AndroidManifest.xml located at PROJECT_PATH\Temp\StagingArea, as well? It will be auto-generated after building the project. Thanks!

1 Like

hello yasirkula!
I use the same androidmanifest work when androidmanifest at plugin/android folder.
but not work at plugin/nativeshare/android
why?

AFAIK, AndroidManifest.xml files located outside of Plugins/Android folder are merged with the main AndroidManifest.xml file only if they are stored in an .aar archive (reference: Unity - Manual: Android App Manifest).

Ps same error code can’t file ContentProvider

Hmm, it appears that there must be a classes.jar file (even if it is empty) inside the AAR file, as well. For reference, putting the attached AAR file (just change its extension from .txt to .aar) to Plugins/NativeShare/Android works for me. However, it’s a hacky solution and I’d really recommend just editing the Plugins/Android/AndroidManifest.xml file, instead.

3447110–273009–ManifestContainer.txt (603 Bytes)

I am new to Unity, so am learning a lot of stuff


Anyway, I’m confused by Share function, you share, but to whom do you share to?

How does person you are sharing something receive what you are sharing?

I do not see an “address” for a recipient.

It shows the list of available apps (Facebook, Twitter, Email, Messages, etc.) and shares the content on the selected app; similar to what happens when you try to share an image in your Gallery/Photos.

So, when I call “Share” function, it should come up with that list?

I ran Android test app, I used your file selector dialog, which works, but when I use native share, and call “Share”, no list pops up
here is simple sample code


This is just test, I don’t share a file, I just “share” text. (“http://google.com”);

public void ShareSomething()
{
NativeShare myNativeShare = new NativeShare();
if (myNativeShare == null)
{
Debug.Log(“shared failed(?)”);
return;
}

myNativeShare.SetText(“http://google.com”);
myNativeShare.Share();
}

Sharing plain text/link is supported and your code looks correct. The null-check is redundant but nevertheless, it shouldn’t prevent NativeShare from working. Can you add a Debug.Log before SetText function and check the device logs (via “adb logcat” or Android Studio) to make sure that it is called? Are there any error messages that appear after that Debug.Log?

No error messages show up in console window


I’ll add before and after Debug.Log message

Ok, that list you talk about shows up now


Is there a file size limit on what is shared?

Android uses native “intent.putExtra( Intent.EXTRA_STREAM, contentUri );” call. Unless it has a file size limit (not that I know of), there should be no limit.

OK, thanks
one more question
do I have to copy the file to temp cache if it is not in my asset folder?