Hey folks
I'm trying to use .onDrop() on a view that needs to accept files. This works fine, I specify a supportedContentTypes
of [.fileURL]
and it works great.
I got a request to add support for dragging the macOS screenshot previews into my app and when I looked at it, they aren't available as a URL, only an image, so I changed my array to [.fileURL, .image]
.
As soon as I did that, I noticed that dragging any image file, even from Finder, calls my onDrop()
closure with an NSItemProvider
that only knows how to give me an image, with no suggestedName
.
Am I missing something here? I had been under the impression that:
- The order of my
supportedContentTypes
indicates which types I prefer (although I now can't find this documented anywhere) - Where an item could potentially vend multiple UTTypes, the resulting NSItemProvider would offer up the union of types that both it, and I, support.
If it helps, I put together a little test app which lets you select which UTTypes are in supportedContentTypes
and then when a file is dragged onto it, it'll tell you which content types are available - as far as I can tell, it's only ever one, and macOS strongly prefers to send me an image vs a URL.
Is there anything I can do to convince it otherwise?
In your sample project you had:
FileRepresentation(importedContentType: .fileURL) { url in
print("Using file representation for: \(url.file)")
return File(url: url.file)
}
// If this DataRepresentation isn't commented out, Finder will always choose this for image files
DataRepresentation(importedContentType: .image) { data in
print("FAILURE, we got data instead of a file URL: \(data)")
return File(url: URL(fileURLWithPath:"/tmp/FAILRE"))
}
Depending on the content type for example public.png.
which is of subclass public.image
. The system will use the DataRepresentation which supports image
content type and not FileRepresentation
because the dropped item is not of type public.file-url
.
If you used FileRepresentation(importedContentType: .image)
then the system would vend the image as a file URL.
I would suggest you review Choosing a transfer representation for a model type to learn about the various transfer representations.