Native Android share to a particular app in Unity

In the previous medium posts “Native Android text sharing in Unity” and “Native Android screenshot/image sharing in Unity” we learnt about how to share some text or screenshot to other apps in a Unity app targeting Android platforms. In this post will target sharing the same text or screenshot to some particular app directly.

This post was originally posted at https://agrawalsuneet.github.io/blogs/native-android-text-sharing-to-particular-app-in-unity/ and reposted on Medium on 14th Aug 2018.

If you have not read the previous medium posts, I would strongly recommend to read them first. You can read them on the links below.

Direct accessibility is a feature that usually boosts the numbers as it reduces the number of clicks in an app. Sharing your score directly on some app can be one of them. Let’s take an example of WhatsApp in our case. There can be a use case where we want to provide a feature in the Unity app where a user can share their score in the form of text or image directly to WhatsApp.

Android provides the functionality where while sharing the text or image, we can provide the package name of the app to which we want to share directly. But before performing direct sharing, we need to confirm that the particular app is installed in the user’s device.

This can be done by getting a list of all the packages installed on users device and checking if the package is we are looking for is available in that list.

The Jave code for this check would be

//Java code

And the Kotlin code would be

fun checkIfAppInstalled(context: Context, packageName: String): Boolean {

val list = context.packageManager
.getInstalledPackages(PackageManager.GET_ACTIVITIES)

for (item in list) {
if (item.packageName.equals(packageName, ignoreCase = true){
return true
}
}

return false
}

But where can I get the package name of the app I want to share on?

You can visit the Play Store page of that particular app and get the package name from the URL of that page.
It will be the marked as id=<packagename>

In the case of WhatsApp, it will be com.whatsapp

Now let’s try to achieve the same functionality of checking whether the sharing app is installed on the user’s device.

The C# code in Unity for the same will be

//C# code

Now that we got a check if the app is installed on the user’s device or not, we can share to the app (WhatsApp in our case) directly by simply setting the package to the intent. Rest all will still be the same as it was in the case of text sharing or screenshot sharing in Unity.

The java code for the same is

//Jave

and the Kotlin code would be

//Kotlin

And the equivalent code in C# for Unity would be

//C# for Unity

And this will open the WhatsApp directly for sharing.

The entire script for Android native sharing to particular app in unity (C#) would be

The above script can be used in any unity app targeting Android platform to share a text or high score natively to any particular app in Android.

Update

That’s all for now. You can read my other interesting posts here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store