SwiftUI and ShareLink - Save button on Share Sheet crashes the app

I have an iOS/iPadOS app. When I run the app on Mac as a Designed for iPad app, Save button on the Share Sheet crashes the app.

I want to share a view with ImageRenderer, but here's an even easier example where I am trying to share an SF Symbol as an image.

ShareLink(item: Image(systemName: "swift"), preview: SharePreview(Text("PNG"), image: Image(systemName: "swift")))

Share button works. It opens Share Sheet. "Add to Photos" works. "Copy" works. I can paste the image file in Finder after I press "Copy". But when I press the "Save" button, my app crashes with the error:

Thread 1: "-[NSItemProvider pathExtension]: unrecognized selector sent to instance 0x600001c1e290"

How to fix this error? How to make saving the image work? Maybe I need additional permissions in plist? I already have "Privacy - Photo Library Additions Usage Description" and everything on iOS and iPadOS works without a problem.

Do you get the same results with just the relevant code in a small test project? If so, please share a link to your test project. That'll help us better understand what's going on. If you're not familiar with preparing a test project, take a look at Creating a test project.

I had the same issue using the share sheet in UIKit. The workaround was to save the image to the file system and present the file URL of this image to the share sheet.

It seems important to use an absolute URL:

guard let url = imageURL?.absoluteURL else { … }

Using a relative URL appeared to create two issues:

  1. Sometimes the given file name e.g. "1.jpg" could be changed to a different (system supplied name) containing a UUID.

  2. When running as an "iOS App on Mac" the share sheet provided a copy option that seemed to fail when presented with a relative URL.

SwiftUI and ShareLink - Save button on Share Sheet crashes the app
 
 
Q