Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Native Share for Android & iOS [Open Source]

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

Thread Status:
Not open for further replies.
  1. Meepo0

    Meepo0

    Joined:
    Nov 24, 2017
    Posts:
    31
    oooh ok now i get it this thread is for Native share srry dude
     
    gnsx likes this.
  2. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
    gnsx likes this.
  3. Meepo0

    Meepo0

    Joined:
    Nov 24, 2017
    Posts:
    31
    Will Definitely try it thx man!!
     
  4. Meepo0

    Meepo0

    Joined:
    Nov 24, 2017
    Posts:
    31
    OMG ITS WORKING THANK YOU VERY MUCH FOR THIS PLUGIN!!!
     
    gnsx likes this.
  5. Meepo0

    Meepo0

    Joined:
    Nov 24, 2017
    Posts:
    31
    Btw the text and subject wont come when sharing on facebook
     
  6. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
    Yeah, Facebook doesn't support text sharing. It's a limitation of the Facebook app itself, though.
     
    gnsx likes this.
  7. mrgreaper

    mrgreaper

    Joined:
    Aug 6, 2017
    Posts:
    18
    Love it and I think your example may actually fix a problem I have yet to even begin work on lol (namely creating an image with text on it for sharing to twitter when the shared text is too long, the example has given me a place to look atleast :) )

    Is there way to tell it not to offer certain options? so for example if the string is too long then not to offer twitter?
     
    gnsx likes this.
  8. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
    This is a feasible feature but it is not implemented in NativeShare. You have to write some native code to implement it.
     
  9. jdbenitez

    jdbenitez

    Joined:
    Feb 11, 2014
    Posts:
    7
    Any idea for this? i can´t share on instagram. I can share in other social platforms

    my code:
    NativeShare nsh = new NativeShare();
    nsh.AddFile(path, "image/jpeg");
    nsh.Share();

    the error in xcode log

    2018-10-04 15:53:13.627 skivalira[251:20208] plugin com.burbn.instagram.shareextension interrupted

    2018-10-04 15:53:13.631 skivalira[251:20208] Hub connection error Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named com.burbn.instagram.shareextension" UserInfo={NSDebugDescription=connection to service named com.burbn.instagram.shareextension}
     
  10. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
    Your code seems correct. Try updating your Instagram, if it is not up-to-date. Otherwise, it is likely an issue with Instagram not supporting UIActivityViewController share action.
     
  11. Lakshi2101

    Lakshi2101

    Joined:
    May 2, 2017
    Posts:
    1
    Hi, the code works fine in a normal unity app. But for arcore, it's not working. I don't see any errors nor the sharing panel!
     
  12. mrgreaper

    mrgreaper

    Joined:
    Aug 6, 2017
    Posts:
    18
    How do you specify an email address? say I want the user to click a button and have it open gmail with the subject "feedback" and the target email as mycoolemail_at_herenotthere_dot_com (not a real email address :p)
    how would I do that?
    Code (CSharp):
    1. new NativeShare().SetTarget("*gmail*").SetSubject("Feedback").?????(mycoolemail_at_herenotthere_dot_com);
    what would ????? be? (also I know *gmail* is not right, but that one I can look up....though if there is a way to simply say email so it uses the phones default email client that may help in case some one has an android phone but wierdly no gmail o_O

    Is what I am wanting simply not possible ? (no problem if thats the case)

    EDIT: not one to sit about waiting, I found a work around :) since I hate it when someone says "solved it" and not say how (specially when your looking for a solution) here is the solution:


    Code (CSharp):
    1. public void sendEmail()
    2.     {
    3.         string toEmail = "mycoolemail_at_herenotthere_dot_com";
    4.         string emailSubject = "Feedback";
    5.         string emailBody = "Feedback:";
    6.         emailSubject = System.Uri.EscapeUriString(emailSubject);
    7.         emailBody = System.Uri.EscapeUriString(emailSubject);
    8.         Application.OpenURL("mailto:" + toEmail + "?subject=" + emailSubject + "&body=" + emailBody);
    9.     }
    then attached that to a button when the button is clicked the phones default email editor opens up and allows the user to type in the feedback, fantastic :)
     
    Last edited: Oct 8, 2018
    gnsx and yasirkula like this.
  13. DevelopmentZone

    DevelopmentZone

    Joined:
    May 6, 2017
    Posts:
    5
    error CS0103: The name `File' does not exist in the current context
    error CS0103: The name `Path' does not exist in the current context
     
  14. Laubak75

    Laubak75

    Joined:
    Jun 5, 2017
    Posts:
    1
    Hi, great little plugin ! Thank you !
    I only have one issue, in the popup share menu on Android I have access to "Es File Explorer" but if I use it to save screenshot in device memory I have a error message ( not supported ). Any idea ?
     
    gnsx likes this.
  15. GameDevH2O

    GameDevH2O

    Joined:
    Jun 28, 2018
    Posts:
    1
    Hello I’m still new to unity & I Downloaded the native share on the asset store & I have a share button but don’t know how to exactly work the NativeShare I tried to drag the script but it’s not letting me
     
  16. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
    @Lakshi2101 I haven't gotten the opportunity to test the plugin on ARCore, so I'm not sure why it is not working. Can you check logcat logs without any filters to see if you can find any possible error messages related to this behaviour?

    @mrgreaper Thanks for posting your solution here! NativeShare doesn't have a method to set the target e-mail(s), so your solution is the way to go in this case.

    @VRLabDeVelopment Add
    using System.IO;
    to the top of your script.

    @Laubak75 I honestly don't. I simply pass the shared data to an Intent object on Android, it is up to the shared application (Es File Explorer) to fetch that data from the Intent. I don't have any application-specific logic in the code.

    @Stanley950 You can write a function that uses NativeShare in one of your scripts and pass that function to the button's On Click event. An example code is available at: https://github.com/yasirkula/UnityNativeShare#example-code
     
    mrgreaper and GameDevH2O like this.
  17. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
    I've created an issue on GitHub for this, I'll update it when I find something new about this issue.
     
  18. Qurious_Media

    Qurious_Media

    Joined:
    Jan 7, 2017
    Posts:
    4
    Hi @yasirkula ,

    I am using your Plugin for Native Share (https://github.com/yasirkula/UnityNativeGallery) for iOS.
    It works well by Capturing proper screenshot through Mail Share. But in whatsapp, image is not shared, it only shares text.
    My next question is instead of captured screenshot, can I share predefined texture which is assigned to one Texture2D....?
     
    gnsx likes this.
  19. Lesha-VH

    Lesha-VH

    Joined:
    Jul 3, 2012
    Posts:
    92
    Could you please add callback for Native Share with result for iOS/Android?
     
  20. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
    @Qurious_Media Unfortunately, some apps don't support sharing an image with a text. Try sharing the image without a text. To share a texture, you can call its EncodeToPNG function and write the resulting byte[] to a temporary file inside Application.temporaryCachePath via File.WriteAllBytes function and share that temporary file.

    @Lesha-VH Please see https://github.com/yasirkula/UnityNativeShare/issues/5
     
    gnsx likes this.
  21. Lesha-VH

    Lesha-VH

    Joined:
    Jul 3, 2012
    Posts:
    92
    @yasirkula

    I think it possible on iOS without big efforts. I found following:

    completionWithItemsHandler must be used with UIActivityViewController to get result with callback.

    Android 5.1 seems also simple - createComposer have result callback...

    I am sorry, I am not iOS/Android developer)))
     
  22. MuhammadTahirUlNasir

    MuhammadTahirUlNasir

    Joined:
    Apr 23, 2017
    Posts:
    1
    hi, if i want to target all sharing apps available on user mobile what i have to do is to setTarget() for each app separately...??? something like I mentioned below...???

    new NativeShare().SetTarget("com.whatsapp");
    new NativeShare().SetTarget("com.Facebook");
    new NativeShare().SetTarget("com.Google);
     
  23. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
    @Lesha-VH I couldn't find anything about createComposer, can you share where you found this info?

    @MuhammadTahirUlNasir Simply, don't use SetTarget at all.
     
  24. NamekGames

    NamekGames

    Joined:
    May 13, 2014
    Posts:
    10
    Hey yasirkula, great asset, was planning to do it myself natively, and then saw this. Great job and thanks!
    One problem, on iOS works fine, but on Android (of course) I get an error.

    E/Unity ( 5666): AndroidJavaException: java.lang.ClassNotFoundException: com.yasirkula.unity.NativeShare
    E/Unity ( 5666): java.lang.ClassNotFoundException: com.yasirkula.unity.NativeShare
    E/Unity ( 5666): at java.lang.Class.classForName(Native Method)
    E/Unity ( 5666): at java.lang.Class.forName(Class.java:204)
    E/Unity ( 5666): at java.lang.Class.forName(Class.java:169)
    E/Unity ( 5666): at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    E/Unity ( 5666): at com.unity3d.player.UnityPlayer.c(Unknown Source)
    E/Unity ( 5666): at com.unity3d.player.UnityPlayer$e$2.queueIdle(Unknown Source)
    E/Unity ( 5666): at android.os.MessageQueue.next(MessageQueue.java:201)
    E/Unity ( 5666): at android.os.Looper.loop(Looper.java:124)
    E/Unity ( 5666): at com.unity3d.player.UnityPlayer$e.run(Unknown Source)
    E/Unity ( 5666): Caused by: java.lang.NoClassDefFoundError: com/yasirkula/unity/NativeShare
    E/Unity ( 5666): ... 9 more
    E/Unity ( 5666): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.yasirkula.unity.NativeShare" on path: DexPathList[[zip file "/data/app/com.makonda.spaceblaster-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.makonda.spaceblaster-1, /vendor/lib, /system/lib]]
    E/Unity ( 5666): at dalvik.system.BaseDexClassLoader.find

    Its a class not found exception. I checked, and in Plugins/NativeShare/Android there is a NativeShare.jar that contains these classes like in the screenshot.

    Do you know what could be the problem?
    Best regards,

    Slobodan
     

    Attached Files:

  25. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
    If your project uses ProGuard, try adding the following line to ProGuard filters:
    -keep class com.yasirkula.unity.* { *; }
     
  26. NamekGames

    NamekGames

    Joined:
    May 13, 2014
    Posts:
    10
    Aaaaaaaah, can't believe I forgot about proguard. Thanks bro
     
    yasirkula likes this.
  27. arturmandas

    arturmandas

    Joined:
    Sep 29, 2012
    Posts:
    240
    Works brilliant for sharing .pdf file. 5 Stars so far (need to check iOS, but Android like a breeze). Callbacks would be nice, but hey, it's free and neat.
     
    yasirkula likes this.
  28. sysads

    sysads

    Joined:
    Mar 23, 2015
    Posts:
    15
    Hi, am new to Unity and just tried to use your plugin but not sure how to go about it.

    I have installed the package, created an empty object and tried to attach the NativeShare script to it but not accepted since its got no MonoBehaviour.

    So can you guide me on how I can add this script to a button so when clicked, it displays sharing options.

    Also where and where do i change the texts?

    Thanks
     
  29. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
  30. sysads

    sysads

    Joined:
    Mar 23, 2015
    Posts:
    15
    Yes I tried using the sample code but kept getting:

    UIScreenMain.cs(158,22): error CS0103: The name `Path' does not exist in the current context

    This is the error line "
    string filePath = Path.Combine( Application.temporaryCachePath, "shared img.png" );"
     
  31. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
    Add
    using System.IO;
    to the top.
     
  32. sysads

    sysads

    Joined:
    Mar 23, 2015
    Posts:
    15
    I did that already but still can't attach share() to my button.

    The question is, am I not suppose to attach "NativeShare.cs" to an object?
     
  33. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
    Yes, you should use NativeShare in your own scripts and attach those script(s) to object(s), if you want. You can add the function in your script that uses NativeShare to the On Click event of the button to trigger the share on a button click.
     
  34. sysads

    sysads

    Joined:
    Mar 23, 2015
    Posts:
    15
    my challenge is when I attach the NativeShare script to an object, it gives an error - can't add script behavior. It needs to derive from Monobehavior.

    So am I suppose to add the monobehavior
     
  35. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
    No, you are not supposed to add NativeShare.cs to your objects. You are supposed to use it in your own scripts/monobehaviours.
     
  36. sysads

    sysads

    Joined:
    Mar 23, 2015
    Posts:
    15
    Ah mamia, now I get you. So I just need to fix the new error now "
    Can't find ContentProvider, share not possible!"

    might be the android xml not in the correct folder
     
    gnsx likes this.
  37. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
  38. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    301
    @yasirkula... I have used Sharing Plugin . It shares the Image In Fb.. and So on. I need one help. When i click the sharing button. The entire Screen is captured and Shared. But I dont want to Share the entire Screen . For eg
    I need to share only the desired position of the screen............

    if the Screen is like this

    1 2 3 4 5 6
    7 8 9 2 1 2
    8 8 8 8 8 8
    9 9 9 9 9 9
    0 0 0 0 0 0

    I should share a screen From 7 8 9 2 1 2 to 9 9 9 9 9 9 rest of them i dont need. Instead of Full Screen. I need to share the selected Screen Shot...

    How Should i do that...
     
  39. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
  40. arturmandas

    arturmandas

    Joined:
    Sep 29, 2012
    Posts:
    240

    Little update: I get an empty size .pdf when trying to share it on iOS (11). Share dialog opens up; I followed necessary build steps. I will be investigating this (maybe the target location is write-locked; both tried Application.temporaryCachePath and Application.persistentDataPath), but if there are some hints for overcoming this, I would welcome them :)
     
  41. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
    Does the pdf appear empty on all applications or does it occur on some applications only?
     
  42. arturmandas

    arturmandas

    Joined:
    Sep 29, 2012
    Posts:
    240
    @yasirkula on all applications. My guess is I am unable to push the bytes to local storage properly - do I need a iOS permissions for that? I will be digging it deeper today, will update you. Thanks!
     
  43. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
    Does
    new FileInfo( filePath ).Length
    return 0? AFAIK, you don't need any permissions to write to temporaryCachePath or persistentDataPath.
     
  44. arturmandas

    arturmandas

    Joined:
    Sep 29, 2012
    Posts:
    240
    @yasirkula I think I solved it. Apparently this code does not work for me for iOS (works fine Android) - 0 file size:

    Code (CSharp):
    1. private IEnumerator ShareFileUsingShareSheetCo()
    2.     {
    3.         mLoadPath = Application.streamingAssetsPath + "/" + mFileName;
    4.         mSavePath = Application.persistentDataPath;
    5.  
    6.         Debug.Log("GOLocalStorageOpenSave mLoadPath " + mLoadPath);
    7.         Debug.Log("GOLocalStorageOpenSave mSavePath " + mSavePath);
    8.  
    9.         WWW www = new WWW(mLoadPath);
    10.         yield return www;
    11.         error = www.error;
    12.  
    13.         byte[] bytes = www.bytes;
    14.         result = "Done, bytes downloaded, File size : " + bytes.Length;
    15.  
    16.         Debug.Log("GOLocalStorageOpenSave result " + result);
    17.         yield return new WaitForSeconds(1f);
    18.  
    19.         //#if UNITY_ANDROID
    20.         string filePath = Path.Combine(Application.temporaryCachePath, mFileName);
    21.         ///#endif
    22.  
    23.         #if UNITY_IOS
    24.         filePath = Path.Combine(mSavePath, mFileName);
    25.         #endif
    26.         File.WriteAllBytes(filePath, bytes);
    27.  
    28.         new NativeShare().AddFile(filePath).SetSubject(mSubject).SetText(mBody).Share();
    29.     }

    But this code works (and does not work for Android, so funny):


    Code (CSharp):
    1. private IEnumerator ShareFileUsingShareSheetStreamingCo()
    2.     {
    3.         mLoadPath = Application.streamingAssetsPath + "/" + mFileName;
    4.         yield return new WaitForEndOfFrame();
    5.  
    6.         new NativeShare().AddFile(mLoadPath).SetSubject("Subject goes here").SetText("Hello world!").Share();
    7.     }
    I will be testing it further. Thanks for support!
     
  45. arturmandas

    arturmandas

    Joined:
    Sep 29, 2012
    Posts:
    240

    Ok works great also for images (iOS 11). Back to 5 stars :)
     
    yasirkula likes this.
  46. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
    Files inside Application.streamingAssetsPath reside in a normal directory on iOS but they reside inside a jar file on Android. The second method doesn't work for Android because files inside a jar file can't be accessed directly on Android (but they can be accessed via WWW). In the first solution, probably adding a "file://" to the beginning of the mLoadPath would resolve the issue for iOS but then it would break it for Android. So you have to do platform dependent compilation there:

    Code (CSharp):
    1. private IEnumerator ShareFileUsingShareSheetCo(string filename)
    2. {
    3.     string mLoadPath = Application.streamingAssetsPath + "/" + filename;
    4. #if UNITY_IOS
    5.     string filePath = mLoadPath;
    6.     yield return new WaitForEndOfFrame();
    7. #else
    8.     string mSavePath = Application.persistentDataPath;
    9.  
    10.     WWW www = new WWW(mLoadPath);
    11.     yield return www;
    12.  
    13.     string filePath = Path.Combine(Application.temporaryCachePath, filename);
    14.     File.WriteAllBytes(filePath, www.bytes);
    15. #endif
    16.  
    17.     new NativeShare().AddFile(filePath).SetSubject("subject").SetText("some text").Share();
    18. }
     
    Last edited: Jun 22, 2020
    arturmandas likes this.
  47. thePortalDude

    thePortalDude

    Joined:
    Jan 9, 2017
    Posts:
    6
    Hi, thanks for your work!

    So far I literally copied and pasted the example code you made in the GitHub page and included the snippet in the AndroidManifest file but nothing happens when the share method is called. I don't know where to find the problem, can you suggest me any possible reason why this is happening?

    Thanks for your help
     
  48. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,801
    Are there any error messages in logcat? Where did you put your AndroidManifest.xml to: Assets/Plugins/Android or Assets/Plugins/NativeShare/Android?
     
  49. julietagur

    julietagur

    Joined:
    Nov 7, 2017
    Posts:
    3
    Sorry I am kind of late. having the same issue here and the AAR is not working, how do you reccomend I edit the Plugins/Android/AndroidManifest.xml file? thanks for the awesome work
     
  50. julietagur

    julietagur

    Joined:
    Nov 7, 2017
    Posts:
    3
    Below a copy of the androidmanifest.xml on Staging Area
    Thanks!
     

    Attached Files:

Thread Status:
Not open for further replies.