download(_:)
Downloads a file from the RCS content server.
Declaration
final func download(_ downloadRequest: RCSService.FileDownloadRequest) async throws -> RCSService.FileDownloadRequest.MetadataParameters
- downloadRequest:
A Filedownloadrequest that indicates the URL to download and its destination path on the local filesystem.
Return Value
A RCSService.FileDownloadRequest.Metadata instance that contains information about the downloaded file.
Discussion
Call this method after receiving an RCSMessage whose content type is RCSMessage.Content.fileTransfer(_:) from the RCS service’s incomingMessageNotifications asynchronous sequence. Use the message’s fileMetadata and optional thumbnailMetadata, both of type RCSFileTransferMetadata, to get the URLs to download. The metadata structure also contains other properties which may be useful for downloading, such as the file size and type.
The following example assumes you received a RCSMessage.FileTransfer instance called fileTransferMessage from the incoming message notifications sequence. It uses the message’s metadata to get a URL with which to call download(_:), and then uses metadata from the download to get a suggestedFileName to help place the file in a permanent location.
let service = TelephonyMessagingSession.shared.rcsService
let cellularServices = try TelephonyMessagingSession.shared.cellularServices
let cellularServiceID = cellularServices[0].id
guard service.isViable(for: cellularServiceID) else { return }
let downloadURL = fileTransferMessage.fileMetadata.url
let temporaryURL = URL.temporaryDirectory
.appendingPathComponent(downloadURL.lastPathComponent)
// Download file.
let request = RCSService.FileDownloadRequest(cellularServiceID: cellularServiceID,
fileURL: downloadURL,
destinationFileURL: temporaryURL)
let downloadMetadata = try await service.download(request)
// Move file to permanent location, using suggested file name.
let receivedURL = URL.documentsDirectory
.appendingPathComponent(downloadMetadata.suggestedFileName ?? "Unknown")
try FileManager.default.moveItem(at: temporaryURL,
to: receivedURL)