Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Native Share for Android & iOS [Open Source]

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

  1. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    281
    Hi Yasirkula

    Thanks For Your Reply..I have use this below code. It will displays the thumbnail of the website. The below code will displays the thumbnail

    i am using the below sharing below code.

    if i use the above code. It displays the thumbnail of the WebSite in whatsapp... When i click the thumbnail view it is target to my website. But in my project it should direct to My App in Playstore.

    When i changed the code og:url to my App Playstore URL

    it is not directed to my Playstore URL. and then if i changed to url thumbnail view is not displaying.

    also not working.


    My objective is if i click the Thumbnail image it should direct to playstore. How can i do it.


    Thank you for your immediate response...
     
  2. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    I don't have any experience with this metadata stuff. I merely shared that stackoverflow entry's link. But why don't you call SetUrl with Play Store page's url directly? Play Store should already support link preview in WhatsApp?
     
  3. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    281
    No if I call playstore address it just displays the url..not thumbnail is not showing...is there any way for thumbnail view
     
  4. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    Not that I know of. The thumbnails come from website metadata, as you've seen. Maybe you can automatically redirect visitors of your own webpage to Play Store page?
     
  5. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    281
    actually many people's visiting my site..so I can't redirect my webpage to play store page.

    Is there any other way?
     
  6. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    You can forward just a single page to Play Store, e.g. yourwebsite.com/mygame.html. As I said, there is no known other way. I'm trying to find a workaround here.
     
  7. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    281
    Thanks yasirkula ..I will try u r idea and update...actually is there any way I can open the playstore app in android mobile..
    If I click.the link the link should open our app playstore page..
     
  8. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    Good question that I don't know the answer of.
     
  9. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    281
    @yasirkula i have found the solution to open the link in playstore.

    it will open the app in playstore. In case if it is not open kinldy delete the playstore cache...

    I need thumbnail view+ meta data display. In case if thumbnail option is not possible atleast i needs this meta data option. I have uploaded the dream11 sample image with this. I need that option.

    yasirukula, i think you have to do some changes in your plugin to enhance this feature. If you provide this feature. Your sharing featuring will reach many peoples.
    . or else if you have some other option let me know.
     

    Attached Files:

  10. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    Thumbnail is completely out of my hands. How do you expect me to enhance it? :)
     
  11. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    281
    @yasirkula ok . i understand. I have enclosed the image(Dream11.png) with this image. I dont want any thumbnail view. Is is possible to display the meta data. only .

    I have to display this.hope you know. anyhow i will quote this
    Rest of the text. I can do it with your plugin. How can i do it.
     

    Attached Files:

  12. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    I keep telling you the same thing. Android OS itself (not NativeShare, the operating system itself) doesn't give us an option to set shared link's thumbnail or metadata. It is entirely auto-generated by WhatsApp and other supported apps by reading the contents of the url. I can't add a feature that Android operating system doesn't support.
     
  13. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    281
    Ok Thanks for your information...
     
    yasirkula likes this.
  14. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    281
    @yasirkula... I want to know no of the list of the shared.... For Eg.. If the user shared using whatsapp. The list of contacts will display. out of 20 the user selected 10 contacts and shared my app.

    is it possible to know how many contacts have been shared or is it possible to get the Whatsapp name or contact no of the person shared(I know it is difficult.. For interested i am asking)... or else

    I want to know how many contacts have been shared.How to know it.
     
  15. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    I honestly don't know if there is a way to fetch that information from OS.
     
  16. EndorphinGames

    EndorphinGames

    Joined:
    Apr 14, 2017
    Posts:
    4
    This might be offtopic, but is there any plugin to open files like ppt/pdf in another app from an button click inside Unity? And this would be for iOS
     
  17. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    There might be but I haven't googled such plugins, so I can't tell for sure. You can create a forum thread on iOS sub-category and a question on Unity Answers to ask the others if you can't find such plugins with a google search.
     
  18. oracleruiz

    oracleruiz

    Joined:
    Apr 23, 2021
    Posts:
    1
    Hi @yasirkula ,

    Thanks for the plugin it is amazing. I do have a question, when I use the share function and I select an app from the list, that app is opened and I share the image. Everything cool at this point. The problem that I see is that selected app runs "inside" the game. If I open the phone view of current apps running I can see the game name app running but the app that I selected for share is shown as the thumbnail. For instance, if I select Twitter, I will see in the task manager list my game listed with the Twitter Timeline as thumbnail. It is like the game created a new instance of this select app to run inside the same process.

    This causes the user is taken out of the game and it has to press back several times until it is returned back to game.
    Is there a way to say to the plugin to create a separated instance of the selected app. So the user can switch back to the game without hitting back several times.

    Regards
     
  19. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    Are you having this issue on Android?
     
  20. El1t3L33t

    El1t3L33t

    Joined:
    Jun 14, 2020
    Posts:
    2
    Hey @yasirkula

    Thank you for the amazing plugin!!

    I'm having some issues simply loading a png into an SMS I'm attempting to send via AddFile..
    I was attempting to use StreamingAssets path since I'm doing this via Android but I must be doing something wrong..


    IE.
    anInstance.AddFile(Path.Combine(Application.streamingAssetsPath, shareImagePath))

    Would appreciate any info can you can provide on this!
     
  21. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    On Android, streaming assets are compressed in apk file. You first need to extract the desired asset to temporaryCachePath. You can do it via WWW, UnityWebRequest or 3rd party assets. After the extraction, simply pass the extracted asset's path to NativeShare.
     
  22. El1t3L33t

    El1t3L33t

    Joined:
    Jun 14, 2020
    Posts:
    2
    Awesome thanks for the tips!

    I did something of that nature and got that working. I'm wondering, is there is any way to confirm that a user actually sent a message through SMS on Android/iOS? I know that there's a callback but I'm wondering what the limitations are there and how accurate that actually is.
     
  23. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    We can't reliably determine if share is successful on Android, as stated in README. On iOS, if callback always returns the correct value for SMS in your device, then I'd expect it to work the same on all iOS devices.
     
  24. tontonpiero

    tontonpiero

    Joined:
    Dec 28, 2013
    Posts:
    6
  25. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    You can add the following queries to your AndroidManifest.xml and you should be good to go (I haven't tested it myself, though):
    Code (CSharp):
    1. <queries>
    2.     <intent>
    3.         <action android:name="android.intent.action.SEND" />
    4.         <data android:mimeType="*/*" />
    5.     </intent>
    6.     <intent>
    7.         <action android:name="android.intent.action.SEND_MULTIPLE" />
    8.         <data android:mimeType="*/*" />
    9.     </intent>
    10. </queries>
    11.  
    The queries element here will cause compiler errors when using old Android build-tools (or maybe Gradle version) so I don't want to add it to the plugin while the policy isn't enforced (i.e. I don't want to deal with users asking about such compiler errors for as long as possible).
     
  26. tontonpiero

    tontonpiero

    Joined:
    Dec 28, 2013
    Posts:
    6
    Thanks for your quick reply!

    I'm not sure to understand, how this will make our app compliant with the new Google policy?
    If there is still the getInstalledPackage() method in your SDK Google will keep rejecting our submissions, no?
     
  27. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    getInstalledPackages method isn't forbidden. Where did you read that it is?
     
  28. tontonpiero

    tontonpiero

    Joined:
    Dec 28, 2013
    Posts:
    6
  29. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    I wouldn't rely on any source other than official Android documentations. I didn't see anywhere in Android documentations that getInstalledPackages is now forbidden.
     
  30. tontonpiero

    tontonpiero

    Joined:
    Dec 28, 2013
    Posts:
    6
    Ok then, thanks for clarifications :)
     
    yasirkula likes this.
  31. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    281
    @yasirkula is there any way i can get the user contacts... Suppose if the user selected contacts name in whatsapp For Eg .. .Alex. I have to say Hi Alex .. and then the sharing information.

    Is there any way i can get it From this sharing plugin?

     
  32. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    Unfortunately not.
     
  33. doukisan

    doukisan

    Joined:
    Nov 26, 2015
    Posts:
    15
    Hi! I used this plugin and it's a saviour! Thank you so much.

    I'm just having an issue with iOS. It all works fine on the Android, but with iOS it's different. It replaces all special characters like accents "á, à, é, etc..." with a "�" in the text, and in Telegram, it only shares the URL, without the text. Am I doing anything wrong? =/

    Here's the code I'm using:

    Code (CSharp):
    1.     public void ShareOnClick ()
    2.     {
    3.         string title = "Share Title";
    4.         string emailSubject = "E-mail Subject";
    5.         string shareText = "Sample téxt with áccents.";
    6.         string url = "https://www.google.com/";
    7.  
    8.         NativeShare sharer = new NativeShare();
    9.  
    10.         //Imagem
    11.         sharer.AddFile(shareImage, "Image");
    12.  
    13.         //Assunto (caso compartilhado por e-mail)
    14.         sharer.SetSubject(emailSubject);
    15.  
    16.         //Corpo do texto
    17.         sharer.SetText(shareText );
    18.  
    19.         //Links (a depender da plataforma)
    20.         sharer.SetUrl(url);
    21.  
    22.         //Título
    23.         sharer.SetTitle(title);
    24.  
    25.         sharer.Share();
    26.     }
     
  34. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    Telegram may not support autofilling both the text and the link, or it may be related to the invalid character issue you are having. Taking this link as reference, you can try:
    • add
      using System.Runtime.InteropServices;
      to NativeShare.cs
    • change this line as follows:
      private static extern void _NativeShare_Share( string[] files, int filesCount, string subject, [MarshalAs(UnmanagedType.LPWStr)] string text, int textLength, string link );
    • change this line as follows:
      extern "C" void _NativeShare_Share( const char* files[], int filesCount, const char* subject, const char16_t* text, int textLength, const char* link )
    • change these lines as follows:
    Code (CSharp):
    1. if( strlen( subject ) > 0 && CHECK_IOS_VERSION( @"7.0" ) )
    2. {
    3.     UNativeShareEmailItemProvider *emailItem = [UNativeShareEmailItemProvider new];
    4.     emailItem.subject = [NSString stringWithUTF8String:subject];
    5.     emailItem.body = [[NSString alloc] initWithBytes: (const void*)text length: sizeof(char16_t) * textLength encoding: NSUTF16LittleEndianStringEncoding];
    6.    
    7.     [items addObject:emailItem];
    8. }
    9. else if( strlen( text ) > 0 )
    10.     [items addObject:[[NSString alloc] initWithBytes: (const void*)text length: sizeof(char16_t) * textLength encoding: NSUTF16LittleEndianStringEncoding]];
     
  35. doukisan

    doukisan

    Joined:
    Nov 26, 2015
    Posts:
    15
    I tried making the changed bot got lots of errors on the MacBook.
    I realised that the issue with the characteres wasn't NativeShare's fault. I'm using Unity's Collab while working on Windows, so I'm only using the Mac for builds. When I opened the C# file in the mac, All the strings with special characteres were like this, so I manually fixed them.
    upload_2021-5-10_16-3-34.png

    I thought about making the url string part of the text string instead of setting the url individually. Would there be any issue with this aproach?
     
  36. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    The url's preview won't be displayed on some social apps but there should be no other issues.
     
  37. doukisan

    doukisan

    Joined:
    Nov 26, 2015
    Posts:
    15
    I fixed all the wrong strings and it worked like a charm. This plugin is flawless, it was all Unity's and Mac's fault. At least it's here in case someone has the same Collab issue. iOS won't display it if the text has this issue, that's why it worked perfectly on Android, but was all messed up in the iOS build.

    For some reason all the texts with special characters were messed up after downloading the project in Unity on MacOS. and after fixing all the characters (I kept the accent, just had to rewrite them in the Mac) it all worked normally.

    Thank you for everything, I didn't expect a fast answer like this, and I really appreciate it.
     
    yasirkula likes this.
  38. pan-cake

    pan-cake

    Joined:
    Feb 27, 2019
    Posts:
    1
    Hi, if I am trying to share large file (more then 2Gb) with dropbox, I get crash with message in logs:
    Terminating app due to uncaught exception 'NSOtherSerializationException', reason: 'can't write int larger than INT_MAX'. System is iOS14. Share to Google drive works well.
    Is there any way to fix it?
     
  39. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
  40. Ikaro88

    Ikaro88

    Joined:
    Jun 6, 2016
    Posts:
    192
    Seems there is a bug on IOS:


    This is the code:

    Code (CSharp):
    1.   public void TakeScreenshot()
    2.     {
    3.         StartCoroutine(CaptureScreen());
    4.     }
    5.     public IEnumerator CaptureScreen()
    6.     {
    7.         yield return null;
    8.         // Take screenshot
    9. string       namefileRecorded = "BV_Screenshot.jpg";
    10.  
    11.   string     fullPathRecorded = Path.Combine(Application.persistentDataPath, namefileRecorded);
    12.  
    13.  
    14.  
    15.         if (File.Exists(fullPathRecorded))
    16.         {
    17.             Debug.Log("exist");
    18.             File.Delete(fullPathRecorded);
    19.             yield return new WaitForEndOfFrame();
    20.         }
    21.  
    22.  
    23.  
    24.                 ScreenCapture.CaptureScreenshot(namefileRecorded);
    25.  
    26.  
    27.  
    28.             Debug.Log("salvato screenshot in" + fullPathRecorded);
    29.    
    30.  
    31.  
    32.         //  yield return new WaitForSeconds(1f);
    33.  
    34.  
    35.  
    36.      string   finalPath = Path.Combine(Application.persistentDataPath, "asd");
    37.  
    38.         string pathcartella = finalPath;
    39.  
    40.         if (!Directory.Exists(pathcartella))
    41.         {
    42.             Directory.CreateDirectory(pathcartella);
    43.             Debug.Log(pathcartella);
    44.         }
    45.  
    46.         finalPath = Path.Combine(finalPath, namefileRecorded);
    47.  
    48.  
    49.         while (!File.Exists(fullPathRecorded))
    50.             yield return null;
    51.  
    52.  
    53.  
    54.  
    55.         if (File.Exists(finalPath))
    56.         {
    57.             Debug.Log("exist2");
    58.             File.Delete(finalPath);
    59.             yield return new WaitForEndOfFrame();
    60.         }
    61.  
    62.  
    63.         Debug.Log("startmove");
    64.  
    65.         File.Move(fullPathRecorded, finalPath);
    66.         Debug.Log("endmove");
    67.  
    68.  
    69.  
    70.         // Show UI after we're done
    71.         yield return new WaitForSeconds(0.1f);
    72.  
    73.         new NativeShare().AddFile(finalPath)
    74.        .SetText("Show this amazing content!")
    75.        .SetCallback((result, shareTarget) => ShareVideoCallback(result))
    76.        .Share();
    77.     }
    78.     public void ShareVideoCallback(NativeShare.ShareResult result)
    79.     {
    80.  
    81.  
    82.     }


    Please help me I am stuck for a while...

    It work ok on telegram but on whatsapp get this:


    2021-05-14 12:20:24.378068+0200 Mira[795:160173] [core] SLRemoteComposeViewController: (this may be harmless) viewServiceDidTerminateWithError: Error Domain=_UIViewServiceErrorDomain Code=1 "(null)" UserInfo={Terminated=disconnect method}
    2021-05-14 12:20:24.868922+0200 Mira[795:160173] Shared to net.whatsapp.WhatsApp.ShareExtension with result: 1
    2021-05-14 12:20:24.870622+0200 Mira[795:160347] [ShareSheet] connection invalidated
    2021-05-14 12:20:43.674870+0200 Mira[795:160173] Could not signal service com.apple.WebKit.WebContent: 113: Could not find specified service
     
  41. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    Is the image shared when you remove SetText? If so, then quoting from FAQ:
     
  42. Ikaro88

    Ikaro88

    Joined:
    Jun 6, 2016
    Posts:
    192
    THANK YOU THAT SOLVED FOR ME!!!

    I not uderstand why on android work with .SetText("Show this amazing content!") but nice to have a solution

    Thanks a LOT!!
     
    yasirkula likes this.
  43. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    281
    Hi yasirkula

    I am using ur coding for sharing. when i try to share my app url and text in ipad using SKYPE.

    When i sharing using skype my SetText text is displaying and my seturl is also displaying...but when i select the user in skype and posted it only my SetURL site is displaying . text is not displaying.

    what i have to do for showing text and image to shown in skype..
     
  44. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    Can you manually append the url to the end of SetText text and remove SetUrl call to see what happens?
     
  45. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    281
    yasirkula , only text is displaying ..no thumbnail view is displaying..in fb also same problem i think i need thumbnail view + text.how can i do it.

    is it any way i can detect...what the user has choose... if i user choose whatsapp i can share the link of the url...

    if the user choose skype or fb i can choose i can without url..

    i want to share text + thumbnail in all (fb, twitter,skype,whatsapp)...how can i do it...

    i have tried some other apps it shares the image with text in skype,twitter,fb,and whatsapp... how others apps

    are doing it.

    and whyi can't do it in this plugin...(your plugin is good...i am asking doubt only)

     
  46. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    Detecting which app is picked and modifying the share content accordingly requires modifying the native iOS source code. I'm not familiar with the process but you can check out this pull request as reference: https://github.com/yasirkula/UnityNativeShare/pull/75/files. I honestly don't know if and how other apps can share to these platforms as you wish. If you can find another free plugin that works correctly, let me know and I'll see how they've achieved it.
     
  47. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    @Alexander21 Just to be sure, removing SetSubject doesn't make a difference, does it?
     
  48. darkhelmet2005

    darkhelmet2005

    Joined:
    May 26, 2014
    Posts:
    8
    Hi Yasirkula,

    I am attempting to blend together your Simple File Browser with this. Mainly, when I save a file from my app using your Simple File Browser asset, it immediately jumps into your Native Share asset using the same file path. I am also using your ShowLoadDialog from the Simple File Browser asset to grab the paths of existing files to then share. This appears to work great for my device on Android 8, however, it fails to share on Android 11. I assume this is because of the different style of file paths Android 11 uses. An example of the error I am seeing is below.

    2021-05-30 00:34:08.496 17244-18181/? E/Unity: Share Error: file does not exist at path or permission denied: content://com.android.externalstorage.documents/tree/primary%3ATestagain/document/primary%3ATestagain%2FLiq_20210529_113449.csv
    (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
    2021-05-30 00:34:08.498 17244-18181/? W/Unity: Share Error: attempting to share nothing!
    (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)

    Do you know if what I am trying to do is possible on Android 11+? Or is there an easier/better way of doing this? Thank you for any insight you can provide!
     
  49. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,265
    You can either modify NativeShare's native source code in Android Studio so that it also accepts Storage Access Framework paths (hard), or copy the file to Application.temporaryCachePath first and then share it from there (easy).
     
  50. darkhelmet2005

    darkhelmet2005

    Joined:
    May 26, 2014
    Posts:
    8
    Copying the file to Application.temporaryCachePath is such an easy and brilliant solution. Thank you so much. Worked like a charm!
     
    yasirkula likes this.
unityunity