yasirkula / UnityNativeShare

A Unity plugin to natively share files (images, videos, documents, etc.) and/or plain text on Android & iOS
MIT License
890 stars 131 forks source link

Image not sharing with iOS app #159

Closed LiveRock closed 1 year ago

LiveRock commented 1 year ago

Hi

I followed the following example:

void Update()
{
    if( Input.GetMouseButtonDown( 0 ) )
        StartCoroutine( TakeScreenshotAndShare() );
}

private IEnumerator TakeScreenshotAndShare()
{
    yield return new WaitForEndOfFrame();

    Texture2D ss = new Texture2D( Screen.width, Screen.height, TextureFormat.RGB24, false );
    ss.ReadPixels( new Rect( 0, 0, Screen.width, Screen.height ), 0, 0 );
    ss.Apply();

    string filePath = Path.Combine( Application.temporaryCachePath, "shared img.png" );
    File.WriteAllBytes( filePath, ss.EncodeToPNG() );

    // To avoid memory leaks
    Destroy( ss );

    new NativeShare().AddFile( filePath )
        .SetSubject( "Subject goes here" ).SetText( "Hello world!" ).SetUrl( "https://github.com/yasirkula/UnityNativeShare" )
        .SetCallback( ( result, shareTarget ) => Debug.Log( "Share result: " + result + ", selected app: " + shareTarget ) )
        .Share();

    // Share on WhatsApp only, if installed (Android only)
    //if( NativeShare.TargetExists( "com.whatsapp" ) )
    //  new NativeShare().AddFile( filePath ).AddTarget( "com.whatsapp" ).Share();
}

It works in Android but in iOS, the image is missing. I can Save to Gallery but when I share via WhatsApp, image is missing. Only text, url appears.

What did I do wrong here?

yasirkula commented 1 year ago

Probably WhatsApp can't share image with prepopulated text on iOS. I'd guess that removing SetText would resolve the issue. Googling "UIActivityViewController whatsapp image text" should yield the same results.

LiveRock commented 1 year ago

Ok, thanks. Tried. Works when I remove

.SetText( "Hello world!" ).SetUrl( "https://github.com/yasirkula/UnityNativeShare" )

Thanks!