Contents

FileRepresentation

A transfer representation for types that transfer as a file URL.

Declaration

struct FileRepresentation<Item> where Item : Transferable

Mentioned in

Overview

Use a FileRepresentation for transferring types that involve a large amount of data. For example, if your app defines a Movie type that could represent a lengthy video, use a FileRepresentation instance to transfer the video data to another app or process.

struct Movie: Transferable {
    let url: URL
    static var transferRepresentation: some TransferRepresentation {
        FileRepresentation(contentType: .mpeg4Movie) { movie in
            SentTransferredFile(movie.url)
        } importing: { received in
            let copy = URL(fileURLWithPath: "<#...#>")
            try FileManager.default.copyItem(at: received.file, to: copy)
            return Self(url: copy)
        }
    }
}

Note that the overall recommendation is to specify the content type that describes the file content as close as possible. For example, if you are sharing a PDF file, declare a FileRepresentation of a UTType.pdf content type, instead of UTType.fileURL or UTType.content so the data can be dragged, shared, or imported to apps that support that data type:

  struct PDFDocument: Transferable {
      var file: URL

      static var transferRepresentation: some TransferRepresentation {
          FileRepresentation(contentType: .pdf) { ...
          } importing: { ... }
      }
  }

It’s efficient to pass data around as a file and the receiver loads it into memory only if it’s required.

Topics

Creating a transfer representation

See Also

File transfer