File Sharing API for Swift Native SDK
Allows users to upload and share files. You can upload any file of up to 5 MB in size. This feature is commonly used in social apps to share images, or in medical apps to share medical records for patients.
When a file is uploaded on a channel
, it's stored and managed using a storage service, and associated with your key. Subscribers to that channel
receive a file event which contains a file ID
, filename
, and optional description
.
Send file
Upload the file to a specified channel.
This method covers the entire process of sending a file, including preparation, uploading the file to a cloud storage service, and post-uploading messaging on a channel.
For the last messaging step, send
internally calls the publish
method to publish a message on the channel.
The published message contains metadata about the file, such as the file identifier and name, enabling others on the channel to find out about the file and access it.
File encryption
The cryptoModule
set in PubNub config will be used for encryption unless you overwrite it using the custom
parameter.
For more information, refer to Crypto module configuration.
Method(s)
func send(
_ content: FileUploadContent,
channel: String,
remoteFilename: String,
publishRequest: PubNub.PublishFileRequest = PubNub.PublishFileRequest(),
custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
uploadTask: @escaping (HTTPFileUploadTask) -> Void,
completion: ((Result<(task: HTTPFileUploadTask, file: PubNubLocalFile, publishedAt: Timetoken), Error>) -> Void)?
)
Parameter | Description |
---|---|
content *Type: PubNub.FileUploadContent Default: n/a | The content to be uploaded. |
channel *Type: String Default: n/a | The channel the file should be uploaded to. |
remoteFilename *Type: String Default: nil | The name of the content when uploaded. |
publishRequest Type: PubNub.PublishFileRequest Default: PubNub.PublishFileRequest() | The request configuration object when the file is published to PubNub. |
custom Type: PubNub.RequestConfiguration Default: PubNub.RequestConfiguration | Custom configuration overrides when generating the File Upload URLRequest . |
uploadTask Type: (HTTPFileUploadTask) -> Void Default: { _ in } | The file upload task executing the upload; contains a reference to the actual URLSessionUploadTask . |
completion Type: ((Result<(task: HTTPFileUploadTask, file: PubNubFile, publishedAt: Timetoken), Error>) -> Void)? Default: n/a | The async Result of the method call. |
Details
/// Content that can be uploaded as a File to PubNub
enum FileUploadContent: CustomStringConvertible, CustomDebugStringConvertible {
/// A URL to an existing file
case file(url: URL)
/// The raw content of a file and the content type that best describes it
case data(_ data: Data, contentType: String?)
/// A stream of data, the content type of that stream, and the total length of the stream in bytes
case stream(_ stream: InputStream, contentType: String?, contentLength: Int)
}
/// Additional information that can be sent during a File publish
struct PublishFileRequest {
show all 56 linesReturns
The completion
handler will respond with a Result
.
Success
A Tuple
containing the HTTPFileUploadTask
that completed, the PubNubFile
or PubNubLocalFile
that was uploaded, and the Timetoken
when it was published.
protocol PubNubLocalFile {
/// The local URL of the file
var fileURL: URL { get set }
/// If the local filenames change this can be used to track the remote filename
var remoteFilename: String { get }
/// The channel the file is associated with
var channel: String { get }
/// PubNub-generated ID for a file
var fileId: String { get }
/// User defined name for a file
show all 29 linesFailure
An Error
describing the failure.
Basic Usage
Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.