ShareLink with custom Transferable struct using FileRepresentation not working as expected

I’m trying to use a ShareLink to share a PDF file. When using the file URL to the PDF directly, everything looks as I would expect:

let fileURL = URL(filePath: "…")
ShareLink(item: fileURL)

But in my actual app, the PDF isn’t available right away and is expensive to create, so I would like to only create it on demand. The documentation on FileRepresentation sounds to me like this is a use case for a custom type that implements Transferable with a FileRepresentation as its transferRepresentation. Before adding all the actual code to generate the PDF, I’m now trying to write a simple type that wraps a file URL and leads to the same results as using the URL directly. And I’m failing miserably.

This is what I tried:

struct PDFFile: Transferable {

    var fileURL: URL

    static var transferRepresentation: some TransferRepresentation {
        FileRepresentation(exportedContentType: .pdf) { pdfFile in
            SentTransferredFile(pdfFile.fileURL)
        }
    }
}

…and then in the view:

let fileURL = URL(filePath: "…")
let pdfFile = PDFFile(fileURL: fileURL)
ShareLink(
    item: pdfFile,
    preview: SharePreview(fileURL.deletingPathExtension().lastPathComponent, image: Image(systemName: "doc.richtext"), icon: Image(systemName: "doc"))
)

Why are no sharing services showing up? Why is there no icon?

I also tried using .fileURL as the exportedContentType, which then makes the sharing services show up, but actually sharing the file leads to an empty file being shared whose filename is garbage (it looks like the system tries to decode the PDF data as a UTF-8 string and use that as the filename).

So my question is: Is it possible to replicate the behavior of sharing a file URL with a custom type that asynchronously generates the data to share?

  • The whole "transferable" API seems pretty strange. Only some cases seem to work and no progress since last year.

  • Thanks or chiming in. Yes, it does seem odd that such a straight-forward use case doesn’t just work.

  • Yeah I have trouble with FileRepresentation and Share, however I am able to drag and drop to the finder. Just check if you are able to drag and drop the finder (on macOS Sonoma). In my case it was a plain text file so I was using the contentType as utf8PlainText. When I had used .text it didn't work. Might be worth filing a feedback and posting the feedback ID.

Add a Comment