Search Unity

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. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @donnaclarkxu88 I don't think it's related to NativeShare :)

    @zhaoxin2022 NativeShare uses OS' share dialog, it doesn't require 3rd party APIs. Usage is pretty straightforward, you can find the example code with both SetText and SetCallback functions on the online documentation. You can remove the unneeded parts like SetSubject and AddFile from the example code.
     
  2. zhaoxin2022

    zhaoxin2022

    Joined:
    Dec 29, 2021
    Posts:
    6
    @yasirkula
    Thanks for your answer.
    On your online documentation, I am able to see the good practice of how to take a screenshot and share it.
    But when I followed your code to share text, native actionsheet didn't appear yet. :(
    In addition, NativeShare doesn't work with Nox Player?

    https://www.file.io/download/pQRlfE3209pX

    In this time, I'd like to ask you for "AddTarget()" method.
    To be honest with you, I don't understand how to set a target to be shared with.
    I wonder if you share some code showing the approach of how to share some text with FB using your plugin?

    new NativeShare().SetText( "Hello world!" ).SetCallback( ( result, shareTarget ) => Debug.Log( "Share result: " + result + ", selected app: " + shareTarget ) )
    .Share();

    Where should I describe FB on the above code?
     
    Last edited: Jan 19, 2022
  3. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Can you test this code as is?
     
  4. zhaoxin2022

    zhaoxin2022

    Joined:
    Dec 29, 2021
    Posts:
    6
    Thanks for your help and !
    It works like a charm!
     
    yasirkula likes this.
  5. zhaoxin2022

    zhaoxin2022

    Joined:
    Dec 29, 2021
    Posts:
    6
    Hi, Volkerku, Can we meet and talk for a while?
     
  6. ajay_unity759

    ajay_unity759

    Joined:
    Dec 10, 2020
    Posts:
    1
    @yasirkula, if i want to add default mail id for whom to send through gmail share then how can i achieve this for android and for ios too?
     
  7. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You can't auto-populate the recipients field with NativeShare.
     
  8. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31

    can you please send the full post?
    I can't fix it like that :(
     
  9. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  10. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Try adding
    File.WriteAllBytes(filePath, bytes);
    to line 30.
     
    Tural-Muslim likes this.
  11. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31


    https://ibb.co/wzvpG4b
    Thank you very much for the answer. He writes that there is a problem with them, but I do not understand why it is problematic. all problem ones bottom part is red
     
  12. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You need to declare those as string variables (you need some level of C# knowledge to work with this plugin by the way). mFileName must be equal to the music file's name (including extension), the rest may stay empty/null.
     
  13. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31

    so can you fix it please. this is beyond my knowledge
     
  14. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I won't help any further in this topic because it isn't related to NativeShare.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5. public class NewBehaviourScript : MonoBehaviour
    6. {
    7.     public string mFileName = "some music.mp3";
    8.     public void ClichkShare()
    9.     {
    10.         StartCoroutine(ShareFileUsingShareSheetCo());
    11.     }
    12.     private IEnumerator ShareFileUsingShareSheetCo()
    13.     {
    14.         WWW www = new WWW(Application.streamingAssetsPath + "/" + mFileName);
    15.         yield return www;
    16.         string filePath = Path.Combine(Application.temporaryCachePath, mFileName);
    17.         File.WriteAllBytes(filePath, www.bytes);
    18.         new NativeShare().AddFile(filePath).Share();
    19.     }
    20. }
     
    Tural-Muslim likes this.
  15. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31


    Thank you very much bro. working very well. I'm sorry for bothering you.
    Allah sendende sevdiklerindede razi olsun.
     
    yasirkula likes this.
  16. SenthilCympl

    SenthilCympl

    Joined:
    Sep 2, 2021
    Posts:
    3
    @yasirkula I want to show Mi share UI when i share image in Mi devices. Is it possible to share with Mi share on Mi devices ?
     
  17. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  18. SenthilCympl

    SenthilCympl

    Joined:
    Sep 2, 2021
    Posts:
    3
    Thanks for update.

    In Mi devices when we select Bluetooth/ NearBy share options, We are getting blank screen while clicking device back button for 1st time. Only on 2nd back click, ApplicationFocus is becoming true.
     
  19. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I'll soon add a parameter to open the shared app in a new task, perhaps that'll resolve this issue, as well. Will let you know when it's ready.
     
  20. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Actually, does calling the following code in Awake/Start help with this issue at all:
    Code (CSharp):
    1. using( AndroidJavaClass ajc = new AndroidJavaClass( "com.yasirkula.unity.NativeShare" ) )
    2.     ajc.SetStatic<bool>( "alwaysUseCustomShareDialog", true );
     
    SenthilCympl likes this.
  21. SenthilCympl

    SenthilCympl

    Joined:
    Sep 2, 2021
    Posts:
    3
    @yasirkula Like addTarget() feature, is there any option where i can skip certain Targets?

    My requirement is i need to skip few targets from list of Sharing options it's showing.
     
  22. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  23. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31




    .opus is WhatsApp's own object, but it doesn't recognize it when sent with this script. What is the reason for this?.

    Is it because of Native Share or is there a mistake in this script?
     
  24. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I haven't heard about this format before. Can you select it in your phone's file browser, and try sharing it from there? Does it work?
     
  25. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31

    yes it works from there
    .wav is like that too.
     
  26. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    In your script, mFileName ends with mp3 but you said its real extension is opus. Can it be the cause of the problem?
     
  27. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31
    No

    sometimes even mp3 is like that
     
  28. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hmm, then it must be because of NativeShare but at the moment, I don't know why. The code you've posted looks fine to me (assuming you haven't modified it). Although unlikely, perhaps there are some logs at runtime in logcat/Xcode that may help pinpoint the issue.
     
  29. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5. //using UnityEngine.Networking;
    6.  
    7.  
    8. public class Share : MonoBehaviour
    9. {
    10.    
    11.     public string mFileName = ".mp3";
    12.  
    13.  
    14.  
    15.     public void ClichkShare()
    16.     {
    17.         StartCoroutine(ShareFileUsingShareSheetCo());
    18.     }
    19.  
    20.     private IEnumerator ShareFileUsingShareSheetCo()
    21.     {
    22.        
    23.         WWW www = new WWW(Application.streamingAssetsPath + "/" + mFileName);
    24.         yield return www;
    25.         string filePath = Path.Combine(Application.temporaryCachePath, mFileName);
    26.         File.WriteAllBytes(filePath, www.bytes);
    27.         new NativeShare().AddFile(filePath).Share();
    28.     }
    29.  
    30.    
    31.     public void ClichkShareW()
    32.     {
    33.         StartCoroutine(ShareFileUsingShareSheetCoW());
    34.     }
    35.  
    36.    
    37.     private IEnumerator ShareFileUsingShareSheetCoW()
    38.     {  
    39.         WWW www = new WWW(Application.streamingAssetsPath + "/" + mFileName);
    40.         yield return www;
    41.         string filePath = Path.Combine(Application.temporaryCachePath, mFileName);
    42.         File.WriteAllBytes(filePath, www.bytes);
    43.        
    44.         if( NativeShare.TargetExists( "com.whatsapp" ) )
    45.         new NativeShare().AddFile( filePath ).AddTarget( "com.whatsapp" ).Share();
    46.     }
    47.  
    48.     public void ClichkShareT()
    49.     {
    50.         StartCoroutine(ShareFileUsingShareSheetCoT());
    51.     }
    52.  
    53.     private IEnumerator ShareFileUsingShareSheetCoT()
    54.     {
    55.         WWW www = new WWW(Application.streamingAssetsPath + "/" + mFileName);
    56.         yield return www;
    57.         string filePath = Path.Combine(Application.temporaryCachePath, mFileName);
    58.         File.WriteAllBytes(filePath, www.bytes);
    59.        
    60.         if( NativeShare.TargetExists( "org.telegram.messenger" ) )
    61.         new NativeShare().AddFile( filePath ).AddTarget( "org.telegram.messenger" ).Share();
    62.     }
    63.  
    64.  
    65. }
     
  30. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31
    I didn't change the script, I just copied
     
  31. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Yeah, assuming mFileName isn't just ".mp3", it looks good to me. I don't have any special cases for .mp3, .png, .txt, .opus or .wav files, I pass them all to the same function without any special per-extension-code. So I'm very clueless about this issue :/
     
  32. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5. using UnityEngine.Networking;
    6.  
    7.  
    8. public class Share : MonoBehaviour
    9. {
    10.    
    11.     public string mFileName = "Clip001.mp3";
    12.  
    13.  
    14.     public void ClichkSharePlay()
    15.     {
    16.         StartCoroutine(SharePlayTest());
    17.     }
    18.  
    19.     private IEnumerator SharePlayTest()
    20.     {
    21.        
    22.         UnityWebRequest www = new UnityWebRequest(Application.streamingAssetsPath + "/" + mFileName);
    23.         yield return www;
    24.         string filePath = Path.Combine(Application.temporaryCachePath, mFileName);
    25.         File.WriteAllBytes(filePath, www.bytes);
    26.         new NativeShare().AddFile(filePath).Share();
    27.     }
    28. }


    Maybe the problem is "WWW" ? Unity says it's obsolete.
    I want to try but what should I type instead "www.bytes"
    if anyone knows, can you tell me?
    so we could understand where the problem was
     
  33. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You can use
    www.downloadHandler.data
    . Hope it really resolves the issue.
     
  34. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31
    there is no error but it didn't work
     
  35. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Please add the following log to the end of SharePlayTest and see what is prints using logcat on the actual device:

    Debug.Log( filePath + " " + new FileInfo( filePath ).Length );
     
  36. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31
    I checked with the debug code you wrote, everything is good. (thanks bro)

    but when i send something using whatsapp on my phone, the files change by themselves.
    Tried on 6 different phones.
    but this problem only occurs when sending via whatsapp.
    It seems that "Native Share" shuffles files when sent via WhatsApp.
    because if i send something with xplore watsap there will be no problem.
    (I wrote this via google translate because my english is bad, i hope i didn't make a mistake)
     
  37. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  38. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31

    same. trouble. :(
     
  39. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    In that case, I don't know what can be done really. The way to share files on Android is using an ACTION_SEND intent and if somehow I've made a mistake creating this intent, share action would at least work fine on other plugins like NatShare. Only a tiny fraction of users should share .opus files since almost no one (including me) have never even heard about this file extension.
     
  40. Deleted User

    Deleted User

    Guest

    Hello is there a way to reward the player once he shares is there any calback?Thank you.
     
  41. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  42. Deleted User

    Deleted User

    Guest

  43. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  44. MTeplytskyi

    MTeplytskyi

    Joined:
    Jun 16, 2020
    Posts:
    26
    Hi! Maybe you can help me with the next problem on Android:
    1) empty project in Unity 2020.3.33f1
    2) added FB Sdk plugin
    3) added your plugin
    4) on device I have FB Lite app installed
    5) while trying to make FB login - login window closes immediately with login result Cancelled
    6) when I remove your package from project - FB login works fine

    P.S. With regular/full FB app installed on device everything works fine
     
  45. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    This test case definitely shows NativeShare faulty but I just can't figure out why this might happen. To my knowledge, the plugin's AndroidManifest shouldn't interfere with FB Lite or any other app in any way and I don't know how the rest of my plugin can cause this issue, honestly :/
     
  46. MTeplytskyi

    MTeplytskyi

    Joined:
    Jun 16, 2020
    Posts:
    26
    I can send you merged AndroidManifest from apk file or the whole project archive, maybe this will help
     
  47. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I don't know what to look for in the AndroidManifest, I simply don't know how AndroidManifest or my code can cause this issue. Test case shows NativeShare faulty but it really doesn't make any sense to me and hence, it's beyond my expertise to figure out what might be causing this issue, unfortunately. I won't be able to help with this issue :/
     
  48. MTeplytskyi

    MTeplytskyi

    Joined:
    Jun 16, 2020
    Posts:
    26
    Hi! I think i found something, its somehow connected with quiries tag in AndoidManifest. When I commented out this tag in your plugins manifest everything login via FB Lite started to work properly
     
  49. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I wouldn't recommend removing the queries tag because otherwise, NativeShare may not work on some Android 11+ devices.
     
  50. OnlyMax

    OnlyMax

    Joined:
    Mar 4, 2013
    Posts:
    3
    Hello! I created a share feature for my mobile game that takes a screenshot that is written to Application.temporaryCachePath and then shares that screenshot with some text using NativeShare. My issue I've been noticing is that even though I'm Destroying the Texture2D screenshot and have the screenshot at Application.temporaryCachePath deleted when you change scenes, the shared image and text still shows up on an app I shared to at a later time.

    To try to explain it better and give an example, I share the screenshot to a second discord account for testing, it was dm'd and accepted, but when I open discord at a later time and go to my dms with the second account, the input field and attachment have my NativeShare text and image again. Is this a NativeShare issue or a coding issue on my end? This is on Android btw. Thank you.
     
Thread Status:
Not open for further replies.