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

    elfasito

    Joined:
    Jul 4, 2017
    Posts:
    51
    Hi yasirkula, great asset, im using it without problem.
    a doubt, now im sharing plain text in whatsapp, selecting inside whatsapp the contact/number what I want to share content.
    but its possible share with a specific number without need of select it inside whatsapp?.
    like the SetSubject, for emails (I not tried it yet).
     
  2. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Unfortunately not.
     
    elfasito likes this.
  3. tinchoz77

    tinchoz77

    Joined:
    Dec 13, 2013
    Posts:
    4
    Hello, how do I share only one download link? Thanks a lot
     
  4. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    new NativeShare().SetText("download link").Share();
     
  5. tinchoz77

    tinchoz77

    Joined:
    Dec 13, 2013
    Posts:
    4
    Thank You yasirluka! Amazing asset!
     
    yasirkula likes this.
  6. unity_2Syr4881vBLX4Q

    unity_2Syr4881vBLX4Q

    Joined:
    Mar 6, 2019
    Posts:
    3
    Hi yasirkula, Thanks for the great asset!
    I`m trying share a PDF file. And I`ve seen on this forum that it`s possible.
    but this one won`t work ↓

    Code (CSharp):
    1.     private IEnumerator ShareFileUsingShareSheetCo(string pdfFilename)
    2.     {
    3.         string mLoadPath = Application.streamingAssetsPath + "/" + pdfFilename;
    4. #if UNITY_IOS
    5.     string filePath = mLoadPath;
    6.     yield return new WaitForEndOfFrame();
    7. #else
    8.         string mSavePath = Application.persistentDataPath;
    9.  
    10.         var www = new WWW(mLoadPath);
    11.         yield return www;
    12.  
    13.         string filePath = Path.Combine(Application.temporaryCachePath, pdfFilename);    
    14.        
    15.         File.WriteAllBytes(filePath, www.bytes);
    16. #endif
    17.         new NativeShare().AddFile(filePath).SetSubject("subject").SetText("some text").Share();
    18.     }
    How can I share a PDF file?
    Do you have any sample code?
     
  7. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Where is the PDF file stored? In StreamingAssets folder, a subfolder or another place? Also try omitting SetSubject and SetText to see if it changes anything.
     
  8. unity_2Syr4881vBLX4Q

    unity_2Syr4881vBLX4Q

    Joined:
    Mar 6, 2019
    Posts:
    3
    Thanks for the reply.
    The files are placed in the StreamingAsset folder. And I omited SetSubject and SetText and tried it.
    pic1.png


    I can share the file, but the contents are empty. ;;
    pic3.png
     
  9. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Interesting. Try using TextAssets. Simply change the file's extension to .bytes, move it outside of StreamingAssets, assign it to a public TextAsset variable and save that TextAsset to a file with textAsset.bytes. Then share that file.
     
    unity_2Syr4881vBLX4Q likes this.
  10. MarcSpraragen

    MarcSpraragen

    Joined:
    Jun 29, 2020
    Posts:
    8
    Hi -- can the tool be used in the other direction? I have Unity as a library embedded in my native Android app, and while running the Unity player I want to load items from the native app's file system (like res or assets).
     
  11. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    NativeShare doesn't have any functionality to receive files/content from outside.
     
  12. unity_2Syr4881vBLX4Q

    unity_2Syr4881vBLX4Q

    Joined:
    Mar 6, 2019
    Posts:
    3
    >yasirkula It worked perfectly. Thank you.

    PDF share
    1.file's extension .pdf -> .bytes
    2.Assign it to a public TextAsset variable
    3.Run this code.↓
    Code (CSharp):
    1.  private IEnumerator PDFShare()
    2.     {
    3.         yield return new WaitForEndOfFrame();
    4.  
    5.         string filePath = Path.Combine(Application.temporaryCachePath, "SamplePDF.pdf");
    6.         File.WriteAllBytes(filePath, _sample.bytes); //_sample == public TextAsset _sample
    7.         new NativeShare().AddFile(filePath).Share();
    8.     }
     
    yasirkula likes this.
  13. ChrissTman

    ChrissTman

    Joined:
    Aug 11, 2015
    Posts:
    10
    Specifically for WhatsApp, you are able to use deep links.
    Have a look here.
    https://pureoxygenlabs.com/how-to-create-a-whatsapp-deep-link-with-a-pre-populated-message/
     
    elfasito likes this.
  14. elfasito

    elfasito

    Joined:
    Jul 4, 2017
    Posts:
    51
    Hello , I appreciate it, thanks.
    this is working well for me:
    Code (CSharp):
    1. public void SendWppToContact()
    2.     {
    3.         string ToWpp = "hello buddy,how are you?";  
    4.         Application.OpenURL("https://api.whatsapp.com/send?phone=123456789" + "&text=" + ToWpp);
    5.  
    6.     }
     
  15. waldgeist

    waldgeist

    Joined:
    May 6, 2017
    Posts:
    388
    Thanks for this great asset. One question here: Is it possible to also *receive* content? My use case is that the user takes a picture and then hands it over to my app. If NatShare cannot do this, are you aware of any other asset that can?
     
  16. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  17. waldgeist

    waldgeist

    Joined:
    May 6, 2017
    Posts:
    388
    @yasirkula Thanks, yes, I found this asset, too. But there is no iOS version of it.
     
  18. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Sorry to hear that. I didn't search for an iOS variant so I'm unaware if one exists.
     
  19. Sparkline

    Sparkline

    Joined:
    Feb 8, 2013
    Posts:
    121
    Hi! Just noticed that I have an error in logcat about denied permission...
    Though sharing works fine. What do i miss?
    denial.png
     
  20. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I've googled this issue numerous times. In my opinion, it just can't be resolved. Since sharing works fine, I'd recommend you to ignore it.
     
  21. makaka-org

    makaka-org

    Joined:
    Dec 1, 2013
    Posts:
    1,023
    Your asset is used in my project now to share photos: AR Masker app.

     
    yasirkula likes this.
  22. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    I do not get the full screenshot (iPhone 11) published in Instagram. Is this something that can be fixed or is it as it is?
     
  23. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Is it possible that the preview is a square but once you share the image, it is full size? If it is not the case and you are using your own screenshot function, also give the example code a try.
     
    pKallv likes this.
  24. Lord_Eniac

    Lord_Eniac

    Joined:
    Jan 28, 2020
    Posts:
    50
    Are the java files used to build the AAR file in the repository? I don't particularly need to build the AAR file, but I like having the option. Were they removed?
     
  25. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  26. LKBrugger

    LKBrugger

    Joined:
    Feb 23, 2021
    Posts:
    2
    Hi there, thanks for the asset!

    I'm just having some issues when I use the Native Share for Android and IOS with some messaging apps, like Whatsapp and Telegram. After I click to share with those aplications, I am redirected to the target app (that's fine! This is the expected behaviour!), but I noticed that the target app is “overriding” my game, in other words, I see my app's name on the title, but it´s showing the screens of the target app (like Whatsapp and Telegram). This is very confusing to the user, because he will not see my game screens anymore. It´s dificult to him to go back to my game. When the target app is Telegram, it's even more confusing because in Whatsapp the application splits in two, my game and Whatsapp, but in Telegram my game "becomes" Telegram and he needs to press "go back" button several times to return to my game . I was hoping that after the target was chosen, it would open the target app (the application splits in two) and I could go back easily to my game (by seeing my game screens when it is selected and not the target app screens).

    Do you know why this is happening? Is there a way to override this behaviour?
     
  27. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Does the same thing happen when sharing from other apps to WhatsApp/Telegram? For example when sharing from WhatsApp to Telegram or from Gallery to WhatsApp.
     
  28. LKBrugger

    LKBrugger

    Joined:
    Feb 23, 2021
    Posts:
    2
    No, it does not. When sharing from other apps it's spliting in two and the screen from the first app remains the same.
     
  29. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hmm.. I really wished that the same issue occurred on other apps, too. I wonder what they do differently.
    ACTION_SEND
    is the standard sharing solution on Android, I don't understand why it is behaving differently in my case. I'm not aware of any way to override this behaviour.
     
  30. philipgen

    philipgen

    Joined:
    Jul 19, 2018
    Posts:
    7
  31. janeshvarmishra

    janeshvarmishra

    Joined:
    Aug 24, 2015
    Posts:
    5
    @yasirkula this plugin support callback for Android ? How to set callback with this this function. Please have a look below which i am using.

    if (NativeShare.TargetExists("com.facebook.katana"))
    {
    new NativeShare().SetTitle("Hello Yasir Kula").AddFile(filePath).SetText("www.google.com").AddTarget("com.facebook.katana").Share();
    }

    Where to set callback function for Android. please help me for this.

    Thanks.
     
  32. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    janeshvarmishra likes this.
  33. leonliu75

    leonliu75

    Joined:
    Jun 18, 2020
    Posts:
    5
    thanks for the great work @yasirkula . I am not sure if you have noticed that the sharing of URL link to some targets on iOS can be improved a little bit, e.g. WeChat. By setting text, there is no title and thumbnail image of the link page shared.

    I played the trick with below modification to your codes, which works perfectly for my case on WeChat and has no negative effects on other targets. Just post it fyi.

    Code (CSharp):
    1. extern "C" void _NativeShare_Share( const char* files[], int filesCount, char* subject, const char* text )
    2. {
    3.     NSMutableArray *items = [NSMutableArray new];
    4.  
    5.     NSURL *url = nil;
    6.     if( strlen( text ) > 0 )
    7.     {
    8.         url = [NSURL URLWithString:[NSString stringWithUTF8String:text]];
    9.         if (url && url.scheme && url.host)
    10.         {
    11.              // this is a valid url, let's share url instead of text
    12.             if( strlen( subject ) > 0 )
    13.                 [items addObject:[NSString stringWithUTF8String:subject]];
    14.         }
    15.         else
    16.         {
    17.             url = nil;
    18.             [items addObject:[NSString stringWithUTF8String:text]];
    19.         }
    20.     }
    21. ...
    22.    
    23.     if ( url )
    24.         [items addObject:url];
    25.  
    The best way may be adding a SetUrl() API to NativeShare, but I do not have time to investigate the Android side and this tiny modification satisfies my use case so far. :)
     
    Last edited: Mar 16, 2021
  34. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  35. Gamemiracle-GM

    Gamemiracle-GM

    Joined:
    Aug 9, 2013
    Posts:
    12
    Hi, still support 2017.4 ?
     
  36. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    5.6 and later is supported.
     
  37. khrysller

    khrysller

    Joined:
    Mar 14, 2019
    Posts:
    125
    Hello. Thanks for this amazing plugin @yasirkula. May I ask you a thing? I would like to implement a reward in my game if the player shared for 5 time. I will store the variable in a playerprefs but how can I work with the method on a if statement that will check if the callback return "shared"?

    I actually have the following script

    Code (CSharp):
    1.     public void ClickShareButton() {
    2.        
    3.         StartCoroutine ("TakeScreenShotAndShare");
    4.     }
    5.  
    6.     IEnumerator TakeScreenShotAndShare()
    7.     {
    8.         yield return new WaitForEndOfFrame();
    9.  
    10.         Texture2D tx = new Texture2D (Screen.width, Screen.height, TextureFormat.RGB24, false);
    11.         tx.ReadPixels (new Rect (0, 0, Screen.width, Screen.height), 0, 0);
    12.         tx.Apply();
    13.  
    14.         string path = Path.Combine(Application.temporaryCachePath, "ImagemCompartilhada.png");
    15.         File.WriteAllBytes (path, tx.EncodeToPNG());
    16.  
    17.         Destroy(tx);
    18.  
    19.         new NativeShare ()
    20.             .AddFile (path)
    21.             .SetSubject ("Test Subject")
    22.             .SetText ("Test Text")
    23.             .SetCallback((result, shareTarget) => Debug.Log("Share result: " + result + ", selected app: " + shareTarget))
    24.             // do a check here if shared increase the playerprefs variable
    25.             .Share ();
    26.     }
    Thanks.
     
  38. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You can check if SetCallback's result is not equal to
    ShareResult.NotShared
    (see documentation for other possible values).
     
    khrysller likes this.
  39. khrysller

    khrysller

    Joined:
    Mar 14, 2019
    Posts:
    125
    Could you please share a simple code checking that case? I am really lost here.Thanks.
     
  40. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Code (CSharp):
    1. .SetCallback((result, shareTarget) =>
    2. {
    3.   if( result != NativeShare.ShareResult.NotShared)
    4.     blabla;
    5. })
     
    ROBYER1 and khrysller like this.
  41. prawn-star

    prawn-star

    Joined:
    Nov 21, 2012
    Posts:
    77
    Hi @yasirkula thanks for the great plugin

    I am just trying to share without an image, where subject and body are strings
    If I select Email on iPhone the subject and text are not coming through?

    Do I need to have AddFile with an empty path for it to work?

    Code (CSharp):
    1.         new NativeShare()
    2.         .SetSubject(subject).SetText(body)
    3.         .SetCallback((result, shareTarget) => DebugLog("Share result: " + result + ", selected app: " + shareTarget))
    4.         .Share();
     
  42. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You don't have to. I wouldn't expect both the body and the subject to be empty. Can you verify that the parameters you've passed aren't empty? Are there any errors in Xcode console?
     
  43. prawn-star

    prawn-star

    Joined:
    Nov 21, 2012
    Posts:
    77
    DOH
    The subject wan't empty but the body was (which is another firebase dynamic link creation issue I have)
    And then I was using Gmail to test which I think has a bug in iOS where the subject line is populated with the body text.
    So nothing was showing as the body text was empty

    So do you know how to fix this iOS Gmail bug?

    Thanks for the quick response
     
  44. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I've updated the plugin on my computer to hopefully resolve the e-mail subject issue but I couldn't test it yet. I'll do so when I have access to the Mac workstation.
     
  45. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  46. Exentro

    Exentro

    Joined:
    Oct 8, 2013
    Posts:
    36
    Hi Yasirkula,

    I have an issue trying the use both NativeGallery and NativeShare.
    Context is an app for toddlers in a museum. At the end of the visit they can use the tablet to take a picture, wich will be printed at the reception using another app (DNP WPS Print).
    I simply save the pic using NativeGallery.SaveImageToGallery and it works nice.
    But I need to load the image back later, then print it. I load it using NativeGallery.GetImageFromGallery then use the path it returns to get the texture using NativeGallery.LoadImageAtPath and finally sharing it with
    Code (CSharp):
    1.                 new NativeShare().AddFile(texture)
    2.                 .AddTarget(packageName)
    3.                 .SetCallback(PrintShareCallback)
    4.                 .Share();
    However, it makes the dnp app crashs when asking the print (but accept the share at first):

    Code (CSharp):
    1. 2021-03-31 15:06:42.862 29860-29860/? E/AndroidRuntime: FATAL EXCEPTION: main
    2.     Process: com.dnp.sharesheet, PID: 29860
    3.     java.lang.IllegalArgumentException: column '_data' does not exist. Available columns: []
    4.         at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:351)
    5.         at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:91)
    6.         at com.dnp.sharesheet.utils.FileUtils.getDataColumn(FileUtils.java:49)
    7.         at com.dnp.sharesheet.utils.FileUtils.getPath(FileUtils.java:22)
    8.         at com.dnp.sharesheet.managers.PrintManager.processQueue(PrintManager.java:65)
    9.         at com.dnp.sharesheet.managers.PrintManager.enqueuePhotos(PrintManager.java:56)
    10.         at com.dnp.sharesheet.activities.PrintActivity.print(PrintActivity.java:283)
    11.         at com.dnp.sharesheet.activities.PrintActivity_ViewBinding$1.doClick(PrintActivity_ViewBinding.java:42)
    12.         at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:18)
    13.         at android.view.View.performClick(View.java:8160)
    14.         at android.widget.TextView.performClick(TextView.java:16221)
    15.         at android.view.View.performClickInternal(View.java:8137)
    16.         at android.view.View.access$3700(View.java:888)
    17.         at android.view.View$PerformClick.run(View.java:30236)
    18.         at android.os.Handler.handleCallback(Handler.java:938)
    19.         at android.os.Handler.dispatchMessage(Handler.java:99)
    20.         at android.os.Looper.loop(Looper.java:246)
    21.         at android.app.ActivityThread.main(ActivityThread.java:8506)
    22.         at java.lang.reflect.Method.invoke(Native Method)
    23.         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    24.         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
    If I leave the application, open the gallery and share it "manually" to the dnp wps print app, it works as intended. Am i missing something ?

    Btw thanks for those awesome assets!
     
  47. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    302
    Hello Yasiruka.. Thanks for you reply. I have downloaded the latest version of your project. When i use the seturl function it just displays the url only. It does not show the thumbnail of the playstore. I am using android version.

    I have doubt. In which site i can create a short link.Could you suggest any site.

    I have enclosed a image( it was shared by some one). So this kind of sharing i needed?

    what i can do for this...
     

    Attached Files:

  48. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @Exentro Using Android's file browser, can you navigate to Android/data/APP_BUNDLE_ID, find the image file there and share it from file browser? I'd expect the same crash to occur because looking at the error, it looks like DNP WPS Print doesn't support sharing images that aren't a part of Gallery, which is an issue on their end (I'd remind you that you aren't sharing the image in the Gallery, you are creating a copy of it by creating a Texture from it and then sharing that copy).

    @Alexander21 SetUrl just appends the link to text on Android, yes. There isn't a method to specifically set url on Android. WhatsApp and other Android apps internally parse the url in the text and try to generate a preview of that url. For this to work, the target website must be able to return metadata for these previews (learn more). So the issue you are having is that, the website you are linking doesn't return this metadata.
     
  49. Exentro

    Exentro

    Joined:
    Oct 8, 2013
    Posts:
    36
    @yasirkula good point :D Also I am impressed how fast you answered!
    I tried to share the pic directly from DCIM folder and it seems their app is loading for eternity.
    Trying to share using the filepath returned by NativeGallery.GetImageFromGallery instead of loading the texture makes the wps print app crashs with the same error.
    Is there a way to share it from unity "as if" it came from the gallery share button ?
    I will contact their support as well to ask them what _data is supposed to contains (and what columns are supposed to be provided).
     
  50. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Files in DCIM are visible in Gallery, you can't reproduce the issue with those images. Files in Android/data/APP_BUNDLE_ID, however, aren't visible in Gallery and you should be able to reproduce the issue with those.

    If NativeGallery can't determine picked image's raw filepath or the filepath is restricted by Android OS, then NativeGallery copies that image's stream to a file in Android/data/APP_BUNDLE_ID. I'm guessing that this is happening in your case. This can't be avoided.

    "_data" is supposed to contain the image's path if the image is in Gallery. But they don't need to know the image's raw filepath. Otherwise other apps like Facebook, Twitter, Instagram, you name it, would also crash. They must update their code to read image via ContentResolver.openInputStream to resolve the issue. They can still use "_data" if they wish, but they must fallback to openInputStream when _data fails. Crashing the app outright is just bad...
     
    Last edited: Mar 31, 2021
Thread Status:
Not open for further replies.