SwiftUI Transferable with FileRepresentation inconsistent share results

1 day ago 1
ARTICLE AD BOX

I have a ShareLink in my SwiftUI view to share a custom Transferable type, and it is mostly working correctly, but when I choose different options in the share sheet, I get some different behaviors.

First, here's the Transferable type implementation:

struct CsvExporter: Sendable { var context: NSManagedObjectContext var outputUrl: URL private func getCsvData() async throws -> Data { // gets data from NSManagedObjectContext, returns as CSV data } } // MARK: - Transferable extension CsvPaymentExporter: Transferable { public static var transferRepresentation: some TransferRepresentation { FileRepresentation(exportedContentType: .commaSeparatedText) { exporter in let data = try await exporter.getCsvData() try data.write(to: exporter.outputUrl) return SentTransferredFile(exporter.outputUrl) } .suggestedFileName { $0.outputUrl.lastPathComponent } } }

For the outputUrl I'm using:

FileManager.default.temporaryDirectory.appendingPathComponent("MyExportFile", conformingTo: .commaSeparatedText)

When I select a sharing option in the resulting share sheet, here's what I get:

Save To Files: works as expected with file name "MyExportFile.csv"

Mail/GMail: also works as expected

Airdrop: Shares file with the correct contents, but named "{random string}.txt"

Messages: Rather than sharing the file, inserts the entire contents of the CSV file into the message entry textfield as plain text.

What am I doing wrong here? Thanks for any help!

Read Entire Article