# iOS v-0.0.9 / Android v-0.0.2

### Imports&#x20;

To integrate the Truvideo SDK, include the following platform-specific imports:

{% tabs %}
{% tab title="C#" %}

```csharp
#if IOS
using UIKit;
using Foundation;
using TruvideoMediaiOS;
#endif

#if ANDROID
using Application = Android.App.Application;
using TruVideoMediaAndroidBinding;
using JsonSerializer = System.Text.Json.JsonSerializer;
using TruVideoMediaLib = TruVideoMediaAndroid.DotnetTruvideoMedia;
#endif
```

{% endtab %}
{% endtabs %}

### Async Callback Wrapper

For handling asynchronous callbacks in Android, use the following method:

{% tabs %}
{% tab title="C#" %}

```csharp
#if ANDROID
private Task<T> ExecuteMediaCallbackAsync<T>(Action<MediaCallback> action)
{
    var tcs = new TaskCompletionSource<T>();

    action.Invoke(new MediaCallback(
        success => MainThread.BeginInvokeOnMainThread(() =>
            tcs.TrySetResult((T)Convert.ChangeType(success, typeof(T)))),
        failure => MainThread.BeginInvokeOnMainThread(() => tcs.TrySetException(new Exception(failure)))
    ));

    return tcs.Task;
}
#endif

#if IOS
private Task<T> ExecuteCallbackAsync<T>(Action<Action<string, NSError>> nativeCall) {
    var tcs = new TaskCompletionSource<T>();
    nativeCall((nsResult, error) => {
        if (error != null)
        {
            tcs.SetException(new Exception(error.LocalizedDescription));
        }
        else {
            // Convert NSString to string
            string result = nsResult.ToString();
            tcs.TrySetResult((T)Convert.ChangeType(result, typeof(T)));
           // tcs.SetResult(result);
        }
    });
    return tcs.Task;
}
#endif
```

{% endtab %}
{% endtabs %}

This function executes image-related actions asynchronously and ensures results are processed on the main thread.

### Upload Media

The uploadFile function facilitates the uploading of photos or videos by accepting the context and the URI of the local file path as parameters.

Our transcriptions module simplifies video transcription. Upload videos effortlessly, initiate transcription, and retrieve results seamlessly, it's ideal for content management, media monitoring, and educational platforms.

**Example Usage :**&#x20;

### implementation

Crete TruvideoMediaUploadHandler  class to get the callback for iOS specific&#x20;

{% tabs %}
{% tab title="C#" %}

```csharp
using System;
using Foundation;
using TruvideoMediaiOS;
using UIKit;
using TruVideoSampleIOSApp;

public class TruvideoMediaUploadHandler : TruvideoMediaUploadDelegate
{
    private readonly MainPage _mainPage;

    public TruvideoMediaUploadHandler(MainPage mainPage)
    {
        _mainPage = mainPage;
    }

    [Export("uploadProgressWithUpdated:")]
    public void UploadProgress(double updated)
    {
        MainThread.BeginInvokeOnMainThread(() =>
        {
            var percentage = Math.Round(updated);
            _mainPage.UpdateProgress($"Upload progress: {percentage}%");
        });
    }
}
```

{% endtab %}
{% endtabs %}

Initalize the `TruvideoMediaUploadHandler` in the constructor of the page&#x20;

{% tabs %}
{% tab title="C#" %}

```csharp
private TruvideoMediaUploadHandler _uploadHandler;

public MainPage(){
    _uploadHandler = new TruvideoMediaUploadHandler(this);
    TruvideoMedia.Shared.Delegate = _uploadHandler;
}
```

{% endtab %}
{% endtabs %}

Utilize upload media to send image and video to the sever

{% tabs %}
{% tab title="C#" %}

<pre class="language-csharp"><code class="lang-csharp">private async void UploadMedia()
{
var tags = new Tags()
                .Set("key", "value")
                .Set("color", "red")
                .Set("order-number", "123")
                .Build();
var metadata = new MediaMetadata()
                .Set("key", "value")
                .Set("list", new List&#x3C;string> { "value1", "value2" })
                .Set("nested", new MediaMetadata()
                    .Set("key", "value")
                    .Set("list", new List&#x3C;string> { "value1", "value2" })
                    .Build())
                .Build();
var tagsString = JsonSerializer.Serialize(tags);
var metadataString = JsonSerializer.Serialize(metadata);
LogTextEditor.Text = "Uploading ....\n";
#if ANDROID
if (_initAuthResult.Equals("Init Authentication Success"))
{
    var mediaResult = await ExecuteMediaCallbackAsync&#x3C;string>(cb => TruVideoMediaLib.UploadMedia(Application.Context, tagsString, metadataString, _cameraList, cb));
    LogTextEditor.Text = "Uploaded media URL:\n" + mediaResult + "\n";
}
else
{
<strong>    await DisplayAlert("TruVideo", "Please authentication first", "OK");
</strong>}
#elif IOS
if (_selectedMedia.Count > 0)
{
    void UploadMediaHandler(MediaResponse response, NSError error)
    {
        MainThread.BeginInvokeOnMainThread(() =>
        {
            if (error != null)
            LogTextEditor.Text = "Upload failed: " + error.LocalizedDescription + "\n";
            else
            LogTextEditor.Text = "Uploaded media URL:\n" + response.UploadedFileURL.AbsoluteString + "\n" +
            "Uploaded media tags:\n" + response.UploadedFileURL.AbsoluteString + "\n" +
            "Uploaded media metaData:\n" + response.Tags + "\n" + response.Metadata +
            "\n" + "Uploaded media type:\n" + response.Type + "\n";
        });
    }
    TruvideoMedia.Shared.UploadMedia(_selectedMedia[0], tagsString, metadataString, UploadMediaHandler);
}
else
{
    LogTextEditor.Text += "No selected media from camera\n";
}
#endif
} 
</code></pre>

{% endtab %}
{% endtabs %}

#### Subscribe to Event&#x20;

```csharp
#if ANDROID
private void subscribeToMediaEvents()
{
    var nativeService = new TruVideoMediaLib();
    var listener = new DataListener();
    // Subscribe to the event
    listener.DataReceived += data =>
        {
            MainThread.BeginInvokeOnMainThread(() =>
        {
            Console.WriteLine($"Received Data: {data}");
        });
    };
    nativeService.SetDataListener(listener);
}
#endif
```

#### Tags&#x20;

The `Tags`class is needed to be created in the project to send tags to the SDK

```csharp
public class Tags
{
    private readonly Dictionary<string, string> _tags = new();

    public Tags Set(string key, string value)
    {
        _tags[key] = value;
        return this;
    }

    public Dictionary<string, string> Build()
    {
        return _tags;
    }
}
```

MediaMetaData

The `MediaMetadata` class is needed to be created in the project to send metadata to the SDK

```csharp
public class MediaMetadata
{
    private readonly Dictionary<string, object> _metadata = new();

    public MediaMetadata Set(string key, object value)
    {
        _metadata[key] = value;
        return this;
    }

    public Dictionary<string, object> Build()
    {
        return _metadata;
    }
}
```
