Send files
Attach files to text messages to share information (support chats) or improve collaboration (social conversations).
Contrary to the Swift SDK, Swift Chat SDK lets users attach not one but multiple files to a single message. They can attach any format of the files, but the size of each file must be at most 5 MB.
Files are uploaded to a message sequentially - the more files you attach, the longer it will take for the files to upload. To improve user experience in your chat app, you can implement a progress bar indicating to the user how many files have already been uploaded.
Requires File Sharing
To store files with PubNub, you must enable File Sharing for your app's keyset in the Admin Portal, choose the storage region, and set the desired file retention period. Consider aligning the file retention period with the retention you set for Message Persistence to store messages and files for the same period of time.
Send files
You can attach files to the message and publish it using the sendText()
method.
Method signature
Head over to the sendText()
method section for details.
Basic usage
Attach two files of different formats to a text message.
// Reference the "support" channel
chat?.getChannel(channelId: "support") { result in
switch result {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
// Join the channel
channel.join(completion: { joinResult in
switch joinResult {
case .success:
// Handle success on joining
// Define the files to be attached
let files = [
InputFile(
name: "document.pdf",
show all 51 linesGet all message files
You can access the files
property of the Message
object to list all files attached to a single message.
Method signature
This method has the following signature:
message.files
Basic usage
List all files attached to a message on the support
channel.
// Retrieve the "support" channel
chat?.getChannel(channelId: "support") { result in
switch result {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
// Fetch the message using the timetoken
channel.getMessage(timetoken: 16200000000000000) { messageResult in
switch messageResult {
case let .success(message):
if let message = message {
// Access the files property
let files = message.files
if !files.isEmpty {
debugPrint("The message contains the following files:")
show all 35 linesGet all channel files
getFiles()
returns all files attached to messages on a given channel.
Method signature
This method takes the following parameters:
channel.getFiles(
limit: Int = 100,
next: String? = nil,
completion: ((Swift.Result<(files: [GetFileItem], page: PubNubHashedPage?), Error>) -> Void)? = nil
)
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
limit | Int | No | 100 | Number of files to return. |
next | String | No | n/a | String token to get the next batch of files. |
Output
Parameter | Type | Description |
---|---|---|
((Swift.Result<(files: [GetFileItem], page: PubNubHashedPage?), Error>) -> Void) | object | Returned object containing these fields: files and page . |
→ files | [GetFileItem] | Array containing file details. |
→ page | PubNubHashedPage | Pagination details. |
→ → next | String | Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off. |
→ → total | Int | Total number of files. |
GetFileItem
contains the following properties:
Parameter | Type | Description |
---|---|---|
name | String | Name of the file, like error-1.jpg . |
id | String | Unique identifier assigned to the file by PubNub, like 736499374 . |
url | String | File's direct downloadable URL, like https://ps.pndsn.com/v1/files/demo/channels/support/files/736499374/error-1.jpg . |
Basic usage
List all files published on the support
channel.
// Reference the "support" channel
chat?.getChannel(channelId: "support") { result in
switch result {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
// Fetch the files attached to messages on the channel
fetchFiles(channel: channel, next: nil)
}
case let .failure(error):
// Handle failure
debugPrint("Failed to get channel details: \(error.localizedDescription)")
}
}
show all 32 linesDelete files
Delete sent files or files from published messages with the deleteFile()
method.
Endpoint limitation
Due to a lack of search functionality in the PubNub Message Persistence API, once you delete the file, you cannot track which historical messages contained the deleted file to remove info about the file from those messages.
Method signature
This method takes the following parameters:
channel.deleteFile(
id: String,
name: String,
completion: ((Swift.Result<Void, Error>) -> Void)? = nil
)
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
id | String | Yes | n/a | Unique identifier assigned to the file by PubNub. |
name | String | Yes | n/a | Name of the file. |
Output
Type | Description |
---|---|
((Swift.Result<Void, Error>) -> Void) | Status code of the response. |
Basic usage
Remove a file named error-screenshot.png
from the support
channel.
// Reference the "support" channel
chat?.getChannel(channelId: "support") { result in
switch result {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
// Delete the file named "error-screenshot.png" from the "support" channel
let fileId = "file-id-to-delete" // Replace this with the actual file ID
let fileName = "error-screenshot.png"
channel.deleteFile(id: fileId, name: fileName) { deleteResult in
switch deleteResult {
case .success:
debugPrint("File '\(fileName)' successfully deleted from 'support' channel.")
show all 26 lines