Upload Media
This guide outlines how to upload images or videos to the server using the TruVideo Media SDK. Whether you're working with captured media or a local file path, the SDK provides a streamlined way to handle uploads in your iOS application.
Prerequisites
Before proceeding, ensure the following:
The TruVideo Core SDK is integrated into your project.
User authentication is successfully completed.
Media files (from the camera module or local file path) are available for upload.
Step-by-Step Guide
Step 1: Create a Media Builder
Start by creating a FileUploadRequestBuilder
using the file URL.
let builder = TruvideoSdkMedia.FileUploadRequestBuilder(fileURL: URL(string: videoPath)!)
Step 2: Set Tags and Metadata (Optional)
Tags and metadata help categorize and describe the uploaded media.
// Tags
builder.setTag("key", "value")
builder.setTag("color", "red")
builder.setTag("orderNumber", "123")
// Metadata
builder.setMetaData("key", "value")
builder.setMetaData("key1", "1")
builder.setMetaData("key2", "[4,5,6]")
Step 3: Build the Upload Request
let request = builder.build()
Step 4: Handle Callbacks for Completion & Progress
Use Combine to handle the upload lifecycle.
let completeCancellable = request.completionHandler
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { completion in
switch completion {
case .finished:
print("Upload completed successfully.")
case .failure(let error):
print("Upload failed:", error)
}
}, receiveValue: { uploadedResult in
print("Uploaded File URL:", uploadedResult.uploadedFileURL)
})
completeCancellable.store(in: &disposeBag)
let progressCancellable = request.progressHandler
.receive(on: DispatchQueue.main)
.sink(receiveValue: { progress in
print("Upload Progress: \(progress * 100)%")
})
progressCancellable.store(in: &disposeBag)
Step 5: Start the Upload
do {
try request.upload()
} catch {
print("Upload initiation failed:", error)
}
Complete Function Example
Here’s a consolidated method that uploads a video file using the TruVideo SDK:
import Combine
import TruvideoSdkMedia
private var disposeBag = Set<AnyCancellable>()
func uploadFile(videoPath: String) {
// 1. Create the upload request
let builder = TruvideoSdkMedia.FileUploadRequestBuilder(fileURL: URL(string: videoPath)!)
// 2. Optional: Add tags and metadata
builder.setTag("color", "red")
builder.setTag("order-number", "123")
builder.setMetaData("category", "demo")
let request = builder.build()
// 3. Handle upload completion
let completeCancellable = request.completionHandler
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { completion in
switch completion {
case .finished:
print("Upload completed successfully.")
case .failure(let error):
print("Upload failed with error:", error)
}
}, receiveValue: { uploadedResult in
print("File uploaded to:", uploadedResult.uploadedFileURL)
})
completeCancellable.store(in: &disposeBag)
// 4. Handle progress updates
let progressCancellable = request.progressHandler
.receive(on: DispatchQueue.main)
.sink(receiveValue: { progress in
print("Upload progress: \(progress * 100)%")
})
progressCancellable.store(in: &disposeBag)
// 5. Trigger the upload
do {
try request.upload()
} catch {
print("Failed to start upload:", error)
}
}
Notes
The SDK uses Combine for asynchronous event handling.
Tags and metadata are optional but useful for organizing and retrieving uploaded media.
Ensure file paths are valid URLs and the media exists before calling
upload()
.
Last updated
Was this helpful?