yasirkula / UnityNativeShare

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

How i can Share PDF File #15

Closed ibenettaleb closed 6 years ago

ibenettaleb commented 6 years ago

I need to now how i can share pdf. file i have files in node js server i have a direct link to them how i can do that

yasirkula commented 6 years ago

You can either share the download link as plain text (SetText), or download the PDF file to the device and then share it as a file (AddFile).

P.S. Sharing the download link as plain text may not work as desired; e.g. if user decides to share on Facebook, it will be shared as a link rather than as an attached file.

ibenettaleb commented 6 years ago

You Can Put C# Example Code.

yasirkula commented 6 years ago

How do you plan to share the PDF? As a link or as a file?

ibenettaleb commented 6 years ago

The PDF are in Node Js serve in Public folder i want to download and put it in piece joint send after that deleted

yasirkula commented 6 years ago
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public void Share( string url )
{
    StartCoroutine( DownloadAndShare( url ) );
}

private IEnumerator DownloadAndShare( string url )
{
    // Credit: https://docs.unity3d.com/Manual/UnityWebRequest-CreatingDownloadHandlers.html
    var uwr = new UnityWebRequest( url, UnityWebRequest.kHttpVerbGET );
    string path = Path.Combine( Application.temporaryCachePath, "temp.pdf" );
    uwr.downloadHandler = new DownloadHandlerFile( path );

    yield return uwr.SendWebRequest();

    if( uwr.isNetworkError || uwr.isHttpError )
        Debug.LogError( uwr.error );
    else
        new NativeShare().AddFile( path ).Share();

    // It may not be safe to delete the file immediately afterwards,
    // maybe it is currently being shared? Instead, you can delete the file
    // at that "path" later in your app's lifecycle or when your app starts
    // File.Delete( path );
}
ibenettaleb commented 6 years ago

Thank you very much #yasirkula you are awesome 👍 :smiley: