ShareLink + FileRepresentation: Can't Save to Files

I'm having trouble getting the "Save to Files" option to work with ShareLink. Here's a simple example:

struct Item: Transferable {
    public static var transferRepresentation: some TransferRepresentation {
        FileRepresentation(exportedContentType: .jpeg) { item in
            // Write a jpeg to disk
            let url = URL.documentsDirectory.appending(path: "item.jpeg")
            let jpegData = UIImage(named: "testImg")!.jpegData(compressionQuality: 1)!
            try! jpegData.write(to: url)
            return SentTransferredFile(url)
        }
    }
}

struct ContentView: View {
    var body: some View {
        ShareLink(item: Item(), preview: SharePreview("Test Item"))
    }
}

The ShareSheet presents all of the usual options, but tapping "Save to Files" briefly presents an empty modal before immediately dismissing.

The following errors are logged to the console:

2022-09-12 14:19:57.481592-0700 ExportTest[3468:1374472] [NSExtension] Extension request contains input items but the extension point does not specify a set of allowed payload classes. The extension point's NSExtensionContext subclass must implement `+_allowedItemPayloadClasses`. This must return the set of allowed NSExtensionItem payload classes. In future, this request will fail with an error.
2022-09-12 14:19:58.047009-0700 ExportTest[3468:1374472] [ShareSheet] cancelled request - error: The operation couldn’t be completed. Invalid argument
Sharing finished with error: Error Domain=NSPOSIXErrorDomain Code=22 "Invalid argument" at SwiftUI/SharingActivityPickerBridge.swift:266
2022-09-12 14:19:58.584374-0700 ExportTest[3468:1379359] [AXRuntimeCommon] AX Lookup problem - errorCode:1100 error:Permission denied portName:'com.apple.iphone.axserver' PID:3472 (
	0   AXRuntime                           0x00000001ca77b024 _AXGetPortFromCache + 932
	1   AXRuntime                           0x00000001ca77d1d8 AXUIElementPerformFencedActionWithValue + 772
	2   UIKit                               0x00000002258b3284 3CB83860-AA42-3F9A-9B6E-2954BACE3DAC + 778884
	3   libdispatch.dylib                   0x0000000105078598 _dispatch_call_block_and_release + 32
	4   libdispatch.dylib                   0x000000010507a04c _dispatch_client_callout + 20
	5   libdispatch.dylib                   0x00000001050820fc _dispatch_lane_serial_drain + 988
	6   libdispatch.dylib                   0x0000000105082e24 _dispatch_lane_invoke + 420
	7   libdispatch.dylib                   0x000000010508fcac _dispatch_workloop_worker_thread + 740
	8   libsystem_pthread.dylib             0x00000001ed9e8df8 _pthread_wqthread + 288
	9   libsystem_pthread.dylib             0x00000001ed9e8b98 start_wqthread + 8
)

Additionally, this error is logged when the ShareSheet is first presented (before tapping Save to Files):

[ShareSheet] Couldn't load file URL for Collaboration Item Provider:<NSItemProvider: 0x282a1d650> {types = (
    "public.jpeg"
)} : (null)

Has anybody had luck getting custom Transferable representations to work with ShareLink?

I would tend to think that the issue is with the try! statement. write(to:) can throw, which is probably the case here. Force unwrapping probably causes the crash.

Here's an example of an implementation that works (at least on my end):

static var transferRepresentation: some TransferRepresentation {

        FileRepresentation(contentType: .myArchive) { archive in
            let url = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true).appendingPathComponent(archive.fileName)
            let res = try JSONEncoder().encode(archive)
            try res.write(to: url, options: .atomicWrite)
            return SentTransferredFile(url)
        } importing: { received in
            let data = try Data(contentsOf: received.file)
            return try JSONDecoder().decode(MyArchive.self, from: data)
        }
    }

I'm also seeing this issue, doing the same thing with the same error messages. Will post back if I'm able to make progress!

I'm also struggling to get this working; same error messages. ShareLink seems quite finicky to get right so far! Will report back if I'm able to work around.

Does anyone have an update on this? I'm facing the same issue. It's even worse that only a few of my users get this error. It works fine for me and most of my users, so I can't even verify if a fix works.

I have this issue as well. Has anyone been able to figure this out?

"cancelled request - error: The operation couldn’t be completed. Invalid argument"

It seems to me that the share sheet doesn't correctly handle the sandbox extension for the file, which causes the Save to Files operation to be canceled. I believe so because, when I use fileImporter to pick a file, and then use ShareLink + FileRepresentation to do Save to Files, the operation does go through successfully.

I’d suggest that you file a feedback report for the share sheet team to investigate – If you do so, or already did, please share your report ID here. I'd route it directly to the team.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

ShareLink + FileRepresentation: Can't Save to Files
 
 
Q