Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Native Share for Android & iOS [Open Source]

Discussion in 'Assets and Asset Store' started by yasirkula, Mar 1, 2018.

  1. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    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: https://www.assetstore.unity3d.com/en/#!/content/112731
    Also available at: https://github.com/yasirkula/UnityNativeShare

    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.
    • Can't share, it says "Can't file ContentProvider, share not possible!" in Logcat
    After building your project, verify that NativeShare's provider tag is inserted in-between the application tags of PROJECT_PATH/Temp/StagingArea/AndroidManifest.xml. If not, please create a new Issue on GitHub.
    • Can't share, it says "java.lang.ClassNotFoundException: com.yasirkula.unity.NativeShare" in Logcat
    If your project uses ProGuard, try adding the following line to ProGuard filters:
    -keep class com.yasirkula.unity.* { *; }


    Enjoy!
     
    Last edited: Nov 30, 2019
    efge, flatsplay, AlonsOh and 5 others like this.
  2. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    Now live on Asset Store!
     
    QashiCheema likes this.
  3. youriding

    youriding

    Joined:
    Jun 2, 2015
    Posts:
    2
    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)
     
  4. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    Simply using new NativeShare().SetText( "http://google.com" ).Share(); should be enough.
     
    youriding likes this.
  5. youriding

    youriding

    Joined:
    Jun 2, 2015
    Posts:
    2
    Works great!
     
    yasirkula likes this.
  6. Nija

    Nija

    Joined:
    Jan 7, 2015
    Posts:
    1
    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.
     

    Attached Files:

    NEVER-SETTLE likes this.
  7. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    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!
     
    siddharth3322 likes this.
  8. Luke-kaser

    Luke-kaser

    Joined:
    Apr 2, 2018
    Posts:
    3
    hello yasirkula!
    I use the same androidmanifest work when androidmanifest at plugin/android folder.
    but not work at plugin/nativeshare/android
    why?
     
  9. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
  10. Luke-kaser

    Luke-kaser

    Joined:
    Apr 2, 2018
    Posts:
    3
    Ps same error code can't file ContentProvider
     
  11. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    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.
     

    Attached Files:

  12. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    63
    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.
     
  13. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    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.
     
  14. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    63
    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();
    }
     
  15. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    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?
     
  16. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    63
    No error messages show up in console window......

    I'll add before and after Debug.Log message
     
  17. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    63
    Ok, that list you talk about shows up now.....
     
  18. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    63
    Is there a file size limit on what is shared?
     
  19. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    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.
     
  20. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    63
    OK, thanks....one more question...do I have to copy the file to temp cache if it is not in my asset folder?
     
  21. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    File must be stored somewhere on local storage; it can be temporary cache path or some other path. But note that you can't directly share an asset in your project; assets are bundled into APK and first you have to somehow extract that asset to local file system.
     
  22. Tenebris_Lab

    Tenebris_Lab

    Joined:
    May 23, 2017
    Posts:
    35
    Brilliant! Thank you. I spend ages composing emails in the background and writing images to the gallery.
     
  23. Tenebris_Lab

    Tenebris_Lab

    Joined:
    May 23, 2017
    Posts:
    35
    Quick question... Is it possible to share/save to gallery on iOS into a specific album?
     
  24. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    Tenebris_Lab likes this.
  25. Fingerway

    Fingerway

    Joined:
    Oct 28, 2015
    Posts:
    7
    Hi,
    I understand that to share some text I have to use ...SetText("http://google.com");
    But how do I attach an image to the message?

    Thanks in advance!
     
  26. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    You can use the AddFile function. See example code for reference.
     
  27. Fingerway

    Fingerway

    Joined:
    Oct 28, 2015
    Posts:
    7
    works like a charm! thank you!
     
  28. Fingerway

    Fingerway

    Joined:
    Oct 28, 2015
    Posts:
    7
    Hi,
    I'm using your asset and it's work very well.
    There are any way to set the text or title in the Share Facebook popup when I choose this app??
    I see that the SetText() and SetTitle() methods only work in Message or Email options.
    If not, the are any other trick to do this?

    Thanks in advance!
     
  29. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    You'll have to use Facebook SDK for that. You can then have 2 share buttons: one for Facebook-only share via Facebook SDK and one for general sharing via NativeShare.
     
  30. unity_mzhJ5ZmNAQqlxg

    unity_mzhJ5ZmNAQqlxg

    Joined:
    Jun 15, 2018
    Posts:
    1
    hi yasirkula, i just want o share video but how share video is already recorded and it's already in gallery.
    i want to share by using it's name not browsing it.
     
  31. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    If you are using NativeGallery plugin to save the video to Gallery, then on Android, you can get the path of the video by calling NativeGallery.GetSavePath (change its visibility to public) before calling SaveVideoToGallery. On iOS, AFAIK, it is not possible to get the path of a video in Photos. You have to keep a separate copy of the video in e.g. Application.temporaryCachePath and share it, instead.

    If you are not using NativeGallery and don't know the path of the saved video, then the best thing you can do is to, again, keep a separate copy of the video at a specific path and share it, instead.
     
    tjPark likes this.
  32. michaelcolin

    michaelcolin

    Joined:
    Jun 12, 2018
    Posts:
    9
    Would there be any reason why instagram doesn't show in the activity viewer on iOS?

    I'm using this component and it works great! -- But, I have instagram installed, but don't see it in the list when I use this.

    And, I did try to add it by tapping the '... more' button at the end of the list. ( see attachment )
     

    Attached Files:

  33. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
  34. michaelcolin

    michaelcolin

    Joined:
    Jun 12, 2018
    Posts:
    9
    Would this fix it :

    NSMutableArray *items = [NSMutableArray new];

    if(mText != NULL && mText.length > 0){

    //[items addObject:mText];

    }

    Commenting the mText?
     
  35. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    You don't have to tweak the source code. Just don't use SetSubject and SetText functions and you should be fine.
     
  36. michaelcolin

    michaelcolin

    Joined:
    Jun 12, 2018
    Posts:
    9
    Thanks!

    Is this what you mean? This breaks the sharing when I comment these out.

    Code (CSharp):
    1.    [DllImport("__Internal")] private static extern void showSocialSharing(ref SocialSharingStruct conf);
    2.  
    3.     public static void CallSocialShare(string title, string message)
    4.     {
    5.         ConfigStruct conf = new ConfigStruct();
    6.         //conf.title = title;
    7.         //conf.message = message;
    8.         showAlertMessage(ref conf);
    9.     }
    10.  
    11.     public static void CallSocialShareAdvanced(string defaultTxt, string subject, string url, string img)
    12.     {
    13.         SocialSharingStruct conf = new SocialSharingStruct();
    14.         //conf.text = defaultTxt;
    15.         conf.url = url;
    16.         conf.image = img;
    17.         //conf.subject = subject;
    18.  
    19.         showSocialSharing(ref conf);
    20.     }
     
  37. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
  38. Drezus

    Drezus

    Joined:
    Oct 14, 2013
    Posts:
    13
    Does it support callbacks? (ex: Is it possible to know wheter or not an user has actually shared the image through a social network or cancelled it before that?)

    If it is, is it possible to tell which services he/she used?

    I'm aiming for a feature in my app that rewards people based on the amount of content sharing they do in different platforms, so I'd need to know both when sharings actually go through completely, and which services were used.
     
  39. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    Callbacks are not supported with this plugin. I believe the only way to support callbacks in a share action would be to use the native SDK of the specific platform, e.g. Facebook SDK.
     
  40. davidfio

    davidfio

    Joined:
    Oct 16, 2015
    Posts:
    3
    Hi, i have problems with sharing a video. I pick the path of the video with NativeGallery and then: "new NativeShare().AddFile(_path).SetText("").Share();" but the sharing panel doesn't appear.
    I'm running in Unity 2018.1.2 and using an Android 7.0.
     
  41. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    Does removing SetText("") change anything? If not, could you please check logcat for any error messages?
     
  42. SocialFreak

    SocialFreak

    Joined:
    Dec 12, 2015
    Posts:
    29
    I have your plugin all setup, AndroidManifest changes made setup the call from a script just like in your example but when I click the share it comes up saying "All apps associated with this action have been turned off, blocked, or are not installed"

    This is strange because I can share just fine from other apps.

    Unity: 2017.4.5f1
    Min API: 16
    Target API: 25

    Ideas? Thanks!
     
  43. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    This is the first time I see this error message. What kind of file(s) are you trying to share? Does it work when you try to share text only? Or when you use the example code?
     
  44. SocialFreak

    SocialFreak

    Joined:
    Dec 12, 2015
    Posts:
    29
    I am just doing a screenshot and then calling:

    "new NativeShare().AddFile( filePath ).SetSubject( "Beat My Beep Beep Blitz Score!" ).SetText( "Get Beep Beep Blitz at: https://play.google.com/store/apps/details?id=com.CSA.beepbeepblitz" ).SetTarget("com.CSA.beepbeepblitz").Share();"

    I recompiled to just do a Text Only call: "new NativeShare().SetText( "http://google.com" ).Share();" and I got a screen that shows the apps to share from so this looks good.

    I have attached a text file with both the AndroidManifest and Share script in it.
     
  45. SocialFreak

    SocialFreak

    Joined:
    Dec 12, 2015
    Posts:
    29
    Duh!!! It was my use of SetTarget. I thought it was to put my app bundle ID but it is to call a specific app for sharing.

    Everything works great once I removed that.

    Thanks for a great asset!
     
    yasirkula likes this.
  46. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    I was about to write the same thing. Glad it works now.
     
  47. SocialFreak

    SocialFreak

    Joined:
    Dec 12, 2015
    Posts:
    29
    Thanks! I left a 5 star review on your plugin!
     
  48. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    I appreciate it, thanks :)
     
    gnsx likes this.
  49. stefanob

    stefanob

    Joined:
    Nov 26, 2012
    Posts:
    44
    Can I also load the files back into the app on iOS? I would like to share a config text file, edit it/replace it and load it again when the app starts the next time.
     
  50. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,167
    Only sharing functionality is provided with NativeShare. If you don't want to let the user select the text file but rather load it from a predefined location like Application.persistentDataPath, you can simply use File.WriteAllText and File.ReadAllText functions.