How to get “share on social” in Unity

I’m writing this post because a reference solution for how to create a Unity button that allows to “share on social” text and images, opening the default “pick a social” screen on mobile apps does not seem to be sufficiently indexed, so my colleague Matteo Bicocchi and I had to struggle in order to put together a complete solution. Here I just re-post information we found elsewhere (in different posts) together with a linear procedure, so I hope to save time for some of you reading :-)

90% of the solution is simply getting this library unity-native-sharing from GitHub and following its setup instructions, in our case the only resulting call is:

NativeShare.Share(I18n.T("SHARE_GRAFFITI_BODY"), mediaPath, null, 
  I18n.T("SHARE_GRAFFITI_SUBJECT"), "image/jpg");

Before finding this simple solution we went through a nightmarish set of experiments that half worked, Android manifest conflicts and Android Studio bizzarries, all stuff that using the above plugin will spare you.

The only case the plugin doesn’t cover is this: on IOS, Instagram by default does not appear among the apps with which you can share your image (it does by default on Android). But the integration to get also Instagram is below.

Share from Unity to social on mobile

Special handling for sharing to Instagram on IOS

For solving the Instagram case we used a different “plugin” that you can deploy together with the one above; it is just a header and implementation (2 files) and a bridge call from Unity, of the kind

  if (Application.platform == RuntimePlatform.IPhonePlayer)
  {
    InstagramShare.PostToInstagram(I18n.T("SHARE_GRAFFITI_SUBJECT"), 
      LocalScreenshot.EncodeToPNG());
  }
  else
  {
    NativeShare.Share(I18n.T("SHARE_GRAFFITI_BODY"), mediaPath, null, 
      I18n.T("SHARE_GRAFFITI_SUBJECT"), "image/jpg");
  }

Now this will still not work by default (you will be warned by XCode on run) because of a permission problem:

canOpenURL: failed for URL: “instagram://app” – error: “This app is not allowed to query for scheme instagram”

Again here is the solution, which is to edit the plist file in XCode. And below there is how to automate the plist change from Unity: put this code in a class saved in an Editor folder:

 [PostProcessBuild]
 public static void ChangeXcodePlist(BuildTarget buildTarget, 
   string pathToBuiltProject)
    {
        if (buildTarget == BuildTarget.iOS)
        {
           [... other changes ...]

            // Change value of CFBundleVersion in Xcode plist
            PlistElementArray array = 
              rootDict.CreateArray("LSApplicationQueriesSchemes");
            array.AddString("instagram");           

            // Write to file
            File.WriteAllText(plistPath, plist.WriteToString());
        }
    }

That’s all. So thank you so much to the solvers of the three problems above, and you should now have a complete solution. :-)

Follow me on Twitter where I post about game design, game development, Unity3d, applied / serious games.

 

Social Share Toolbar

Speak Your Mind

*