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

    zyrygon

    Joined:
    Nov 25, 2016
    Posts:
    3
    Hi @yasirkula , thanks for great plugin! Is it possible to show the image (such as screenshot of a game) in the share-window that pops up, so the user knows what they're sharing? Sorry if this question has already been answered, couldn't find it.
     
  2. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Can you share a sample screenshot of how it is supposed to look (e.g. when sharing from other apps)?
     
  3. zyrygon

    zyrygon

    Joined:
    Nov 25, 2016
    Posts:
    3
    Like this on Android, not completely sure about iOS.
     

    Attached Files:

  4. zyrygon

    zyrygon

    Joined:
    Nov 25, 2016
    Posts:
    3
    Or is it necessary to link to a website to get that, so not possible with a shared local file?
     
  5. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I've tried googling the issue with a few keywords but couldn't find a result. Currently, I don't know how that icon can be customized :/
     
  6. ALL-CAPS

    ALL-CAPS

    Joined:
    Jun 23, 2014
    Posts:
    9
    Hey Yasirkula, for pdf's is there a way to open them when they are shared, it seems that I can save and append it in other apps, but I would like to share and open the pdf in one go, not share to drive and then have to go and find and open it myself. Is that possible?
     
  7. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    It isn't possible with NativeShare. You need to find a method open PDF files and call it inside NativeShare's callback function.
     
  8. anhnhgtv

    anhnhgtv

    Joined:
    May 7, 2021
    Posts:
    3
    Hi @yasirkula,

    Thanks you for great plugin. I have 1 question about plugin on iOS. On iOS I missing share's content, I try share everywhere but only show link. Everything is fine on android.

    p/s: sorry about my English not good :D
     
  9. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @anhnh_unity206 Can this be the cause?
     
  10. anhnhgtv

    anhnhgtv

    Joined:
    May 7, 2021
    Posts:
    3
    thank you. i fixed this. I add link to text content and share only text content. This is ok =))
     
    yasirkula likes this.
  11. canoxd69

    canoxd69

    Joined:
    Mar 20, 2021
    Posts:
    7
    Hi, we can not send image on ios. But we send easily texts on ios devices. What is the problem of image? In this issue in whatsapp platform.
     

    Attached Files:

    mucahiterenozkur likes this.
  12. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Quoting the FAQ:
    I'm confused about the screenshot. Why are photo library descriptions empty? Why is app category 'Save media to Photos'? But I don't think these are related to the issue.
     
  13. canoxd69

    canoxd69

    Joined:
    Mar 20, 2021
    Posts:
    7
    namespace NativeShareNamespace
    {
    public class NSPostProcessBuild
    {
    private const bool ENABLED = false; // we set this part false for build on ios
    private const string PHOTO_LIBRARY_USAGE_DESCRIPTION = "Privacy - Photo Library Additions Usage Description"; //"The app requires access to Photos to save media to it.";
    #if UNITY_IOS
    #pragma warning disable 0162
    [PostProcessBuild]
    public static void OnPostprocessBuild( BuildTarget target, string buildPath )
    {
    if( !ENABLED )
    return;
    if( target == BuildTarget.iOS )
    {
    string plistPath = Path.Combine( buildPath, "Info.plist" );
    PlistDocument plist = new PlistDocument();
    plist.ReadFromString( File.ReadAllText( plistPath ) );
    PlistElementDict rootDict = plist.root;
    rootDict.SetString( "NSPhotoLibraryUsageDescription", PHOTO_LIBRARY_USAGE_DESCRIPTION );
    rootDict.SetString( "NSPhotoLibraryAddUsageDescription", PHOTO_LIBRARY_USAGE_DESCRIPTION );
    File.WriteAllText( plistPath, plist.WriteToString() );
    }
    }
    #pragma warning restore 0162
    #endif
    }
    }


    NSPostProcessBuild this script we modified just enabled part and PHOTO_LIBRARY_USAGE_DESCRIPTION
     
    mucahiterenozkur likes this.
  14. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    When you set ENABLED to false, the script doesn't modify Xcode project in any way. So you must've made those modifications manually. You should've changed the values of photo library descriptions whereas you've modified App Category instead.
     
  15. canoxd69

    canoxd69

    Joined:
    Mar 20, 2021
    Posts:
    7
    Okey, so we should not modified this script. Just changed the values of photo library descriptions.
     
    mucahiterenozkur likes this.
  16. canoxd69

    canoxd69

    Joined:
    Mar 20, 2021
    Posts:
    7
    In NSPostProcessBuild script
    we change
    public bool AutomatedSetup = true;
    public string PhotoLibraryUsageDescription = "The app requires access to Photos to save media to it.";
    like this right ?
     
    mucahiterenozkur likes this.
  17. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You need to change those values from Project Settings/yasirkula/Native Share (it is mentioned in the documentation). I'd recommend you to keep Automated Setup enabled, yes. As for Photo Library Usage Description, feel free to change it if the default text isn't good enough for your project.
     
  18. canoxd69

    canoxd69

    Joined:
    Mar 20, 2021
    Posts:
    7
    what do we write for app category in xcode?
    And also In project settings we can not find Project Settings/yasirkula/Native Share this path.
     
    mucahiterenozkur likes this.
  19. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    App Category is irrelevant to NativeShare, it doesn't matter. Can you update the plugin to the latest version and see if it fixes the Project Settings issue? If not, download the latest version from GitHub instead. In the worst case, you can keep everything as is and modify those strings from Xcode after each build.
     
  20. canoxd69

    canoxd69

    Joined:
    Mar 20, 2021
    Posts:
    7
    Thanks for answering we solve this issue. Like this
    #if PLATFORM_IOS
    new NativeShare().AddFile(filePath)
    //.SetSubject("URL:").SetText(QRDATA.text)
    .SetCallback((result, shareTarget) => Debug.Log("Share result: " + result + ", selected app: " + shareTarget))
    .Share();

    #endif
    We can not use setSubject and setText methods after that we send image easily. In my opinion if we use both of methods ios can not send image. Is there any way to send both images and texts?
     
    mucahiterenozkur likes this.
  21. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I really wish there was a way. If there is, I'm unaware of it.

    P.S. You can try other free plugins (example) and see if they can share image with a text. If they can, please let me know. I haven't tried other plugins myself.
     
  22. canoxd69

    canoxd69

    Joined:
    Mar 20, 2021
    Posts:
    7
    Okay, thanks for helping again.
     
    yasirkula likes this.
  23. jooyoung_unity

    jooyoung_unity

    Joined:
    Jun 2, 2021
    Posts:
    8
    hi in current ios 14 and later versions and ipad

    new NativeShare() .SetText("test") .Share();

    There is a phenomenon that it is not possible to share in Test Flight. [ShareSheet] connection invalidated is displayed, and the example code is the same. Can you solve this problem?
    Strangely, it works fine in the xcode simulator.
     
  24. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    If you share a text from WhatsApp to Test Flight, does it work? I'm trying to understand if it can be an OS-level bug.
     
  25. jooyoung_unity

    jooyoung_unity

    Joined:
    Jun 2, 2021
    Posts:
    8
    When sharing is executed, the sharing screen itself does not appear. function is unresponsive
     
  26. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    What logs can you see in the Xcode console when you trigger the share function?
     
  27. jooyoung_unity

    jooyoung_unity

    Joined:
    Jun 2, 2021
    Posts:
    8
    When I click the Share button, only [ShareSheet] connection invalidated appears. No other logs were output.
     
  28. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    When I google the issue, I usually see results like "the share sheet was squished and did not have any buttons in it". Did the share sheet absolutely not appear at all, or did it briefly appear, or did anything seem to appear at all?
     
  29. jooyoung_unity

    jooyoung_unity

    Joined:
    Jun 2, 2021
    Posts:
    8
    Nothing came out but it still works fine in the xcode simulator. Does Apple require a release or other confirmation? Currently only text and internal screenshots are shared, so no permissions are used.
     
  30. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I can't find anything about this issue on google, I've searched with several keywords but unfortunately no results showed up. To my knowledge, you don't need a confirmation or permission from Apple.
    • Can you reproduce the issue on a new Unity project?
    • Is there anything I should know about your original project? Like, is it a Unity as a Library project?
    • Do other share plugins work fine (example)?
     
  31. jooyoung_unity

    jooyoung_unity

    Joined:
    Jun 2, 2021
    Posts:
    8
    I will test it with another sharing system and a new project and share the results.

    + It seems to conflict with the ATT (App Tracking Transparency) setting when using ads in ios version 14 or higher.
     
    Last edited: Jul 27, 2021
  32. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I can't tell whether NativeShare or ATT is to blame here. I'd recommend you to ask ATT's author why the 2 plugins might conflict. Since ATT is (probably) a paid asset, I can't look at their source code but they can look at my source code freely.
     
  33. jooyoung_unity

    jooyoung_unity

    Joined:
    Jun 2, 2021
    Posts:
    8
    Currently, you have contacted ATT for advertising settings. I will share it when confirmed
     
  34. jooyoung_unity

    jooyoung_unity

    Joined:
    Jun 2, 2021
    Posts:
    8
    There was a problem with the header file that was imported to the ad facebook page in iOS.
    The error you asked above has nothing to do with Share. Sorry.
     
    yasirkula likes this.
  35. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Is the NativeShare issue resolved after fixing the header file problem?
     
  36. jooyoung_unity

    jooyoung_unity

    Joined:
    Jun 2, 2021
    Posts:
    8
    solved without problem
     
    yasirkula likes this.
  37. AkhmedAbasov

    AkhmedAbasov

    Joined:
    Mar 13, 2014
    Posts:
    163
    Hey! I get a crash app and this error:

    Code (CSharp):
    1. 07-29 18:45:02.062: E/AndroidRuntime(5674): FATAL EXCEPTION: main
    2. 07-29 18:45:02.062: E/AndroidRuntime(5674): Process: com.tycumkari.carjump, PID: 5674
    3. 07-29 18:45:02.062: E/AndroidRuntime(5674): java.lang.RuntimeException: Unable to resume activity {com.tycumkari.carjump/com.yasirkula.unity.NativeShareCustomShareDialogActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
    4. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3430)
    5. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3470)
    6. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2733)
    7. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at android.app.ActivityThread.-wrap12(ActivityThread.java)
    8. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
    9. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at android.os.Handler.dispatchMessage(Handler.java:102)
    10. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at android.os.Looper.loop(Looper.java:154)
    11. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at android.app.ActivityThread.main(ActivityThread.java:6121)
    12. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at java.lang.reflect.Method.invoke(Native Method)
    13. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
    14. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
    15. 07-29 18:45:02.062: E/AndroidRuntime(5674): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
    16. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at com.yasirkula.unity.NativeShareContentProvider.parsePathStrategy(NativeShareContentProvider.java:5)
    17. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at com.yasirkula.unity.NativeShareContentProvider.getPathStrategy(NativeShareContentProvider.java:3)
    18. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at com.yasirkula.unity.NativeShareContentProvider.getUriForFile(NativeShareContentProvider.java:1)
    19. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at com.yasirkula.unity.NativeShare.CreateIntentFromBundle(NativeShare.java:30)
    20. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at com.yasirkula.unity.NativeShareCustomShareDialog.onCreateDialog(NativeShareCustomShareDialog.java:1)
    21. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at android.app.DialogFragment.getLayoutInflater(DialogFragment.java:406)
    22. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:995)
    23. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1171)
    24. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at android.app.BackStackRecord.run(BackStackRecord.java:816)
    25. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1578)
    26. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at android.app.FragmentController.execPendingActions(FragmentController.java:371)
    27. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at android.app.Activity.performResume(Activity.java:6821)
    28. 07-29 18:45:02.062: E/AndroidRuntime(5674):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3407)
    29. 07-29 18:45:02.062: E/AndroidRuntime(5674):     ... 10 more
    Code (CSharp):
    1. private IEnumerator TakeScreenshotAndShare()
    2.         {
    3.             yield return new WaitForEndOfFrame();
    4.  
    5.             Texture2D ss = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
    6.             ss.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    7.             ss.Apply();
    8.  
    9.             string filePath = Path.Combine(Application.temporaryCachePath, "shared_img.png");
    10.             File.WriteAllBytes(filePath, ss.EncodeToPNG());
    11.  
    12.             // To avoid memory leaks
    13.             Destroy(ss);
    14.  
    15.             new NativeShare().AddFile(filePath)
    16.                 .SetSubject("Car jump!").SetText("I was able to get through to the level " + LevelManager.Instance.CurrentLevel).SetUrl("https://play.google.com/store/apps/details?id=com.test.test")
    17.                 //.SetCallback((result, shareTarget) => Debug.Log("Share result: " + result + ", selected app: " + shareTarget))
    18.                 .Share();
    19.  
    20.             // Share on WhatsApp only, if installed (Android only)
    21.             //if( NativeShare.TargetExists( "com.whatsapp" ) )
    22.             //    new NativeShare().AddFile( filePath ).AddTarget( "com.whatsapp" ).Share();
    23.         }
    Read and write permissions have been granted.
     
  38. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I need to see the final AndroidManifest.xml which is embedded in the generated APK. Can you use Android Studio or any other method (export androidmanifest from apk) and send the AndroidManifest to me via private message?
     
  39. AkhmedAbasov

    AkhmedAbasov

    Joined:
    Mar 13, 2014
    Posts:
    163
    I have a standard manifest when using admob:

    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <!-- GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN-->
    3. <manifest
    4.     xmlns:android="http://schemas.android.com/apk/res/android"
    5.     package="com.unity3d.player"
    6.     xmlns:tools="http://schemas.android.com/tools">
    7.     <application>
    8.         <activity android:name="com.unity3d.player.UnityPlayerActivity"
    9.                   android:theme="@style/UnityThemeSelector">
    10.             <intent-filter>
    11.                 <action android:name="android.intent.action.MAIN" />
    12.                 <category android:name="android.intent.category.LAUNCHER" />
    13.             </intent-filter>
    14.             <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    15.         </activity>
    16.      
    17.      
    18.  
    19.     </application>
    20.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    21.     <uses-permission android:name="android.permission.INTERNET" />
    22.  
    23.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    24.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    25.     <uses-permission android:name="android.permission.VIBRATE" />
    26.  
    27.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" tools:node="remove" />
    28.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" tools:node="remove" />
    29. </manifest>
    30.  
     
  40. vasquesoto

    vasquesoto

    Joined:
    Jul 30, 2021
    Posts:
    3
    Does anyone know how to check the value of the result variable and put it in a conditional?
    Thanks for everything :)
     
  41. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  42. vasquesoto

    vasquesoto

    Joined:
    Jul 30, 2021
    Posts:
    3
    Thanks for answering!
    I am a newbie in this, I want to give an award for sharing the game, but if you do not share it you do not receive it, can it be done?
    In the example, only debug.log is used to display the result and I don't understand how to pass that to a variable :(
     
  43. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Code (CSharp):
    1. .SetCallback( ( result, shareTarget ) =>
    2. {
    3.     if( result != NativeShare.ShareResult.NotShared )
    4.         GiveRewardToPlayer();
    5. } )
     
  44. vasquesoto

    vasquesoto

    Joined:
    Jul 30, 2021
    Posts:
    3
    Thank you very much you are a hero !!! :):):D
     
    yasirkula likes this.
  45. dvelasco

    dvelasco

    Joined:
    Jul 23, 2014
    Posts:
    7
    Hello! Great plugin, thank you very much! I have an issue that maybe has a workaround, though:

    When I share an image+text, the plugin goes into Android sharing, as expected. Everything works.
    Unfortunately, it will exit the app once this is done, as opposed to returning to the main menu, for example.

    Is there a way to do this? I want to return to the app after it does the sharing, instead of the Android OS.

    Thanks!!
     
  46. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Can you reproduce the issue on different Android devices? If there are no crash logs that mention NativeShare in logcat, then I don't think I can resolve this issue. At least, I suspect that this issue occurs on only a single device for unknown reasons.
     
  47. dvelasco

    dvelasco

    Joined:
    Jul 23, 2014
    Posts:
    7
    So the expected behavior of the plugin is to exit the program once the share is executed? Sincerely asking.

    Actually, I´ll try and be more descriptive. When you activate the app to send the image (whatsapp, for example), you send it, and remain in whatsapp. You need to close it and switch back to the original app, or just switch back to the original app.

    Is there a way to switch automatically, right after the image is sent? Probably not, as it seems like the images and text are just passed along, and, once in the native program, the native program (whatsapp, in this case) takes over. This is expected, right?
     
    Last edited: Aug 4, 2021
  48. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Yes, this is expected.
    Of course not (unless you mean exiting WhatsApp after share). Sharing is not supposed to exit your own program.
     
  49. ItsAmee

    ItsAmee

    Joined:
    Nov 22, 2012
    Posts:
    11
    I'm getting an error on Android I don't get on iOS ;-

    (my debug) Screen.width:1920 Screen.height: 1080

    ReadPixels was called to read pixels from system frame buffer, while not inside drawing frame.
    UnityEngine.Texture2D:ReadPixels(Rect, Int32, Int32)

    [d3d11] attempting to ReadPixels outside of RenderTexture bounds! Reading (0, 0, 1920, 1080) from (889, 320)

    I'm rendering 1 camera to a renderTexture then trying to share that.
    Any suggestions?
     
  50. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    ItsAmee likes this.
Thread Status:
Not open for further replies.