I'm trying to make a macOS app using SwiftUI that supports dragging arbitrary files from the app into finder. However, I'm getting this error:
"Sandbox extension data required immediately for flavor public.file-url, but failed to obtain."
I'm looking through the entitlements and not finding anything obvious here.
I've tried various forms of NSItemProvider()
:
Try 1:
let itemProvider = NSItemProvider(item: image.data as NSSecureCoding, typeIdentifier: image.uniformType.identifier)
// Tried presenting as data
itemProvider.registerDataRepresentation(for: .fileURL, visibility: .all) { completion in
...
}
// Tried presenting as file
itemProvider.registerFileRepresentation(for: .fileURL, visibility: .all) { completion in
...
}
Try 2:
let itemProvider1 = NSItemProvider(contentsOf: tempFileUrl, contentType: .fileURL)
Tried using this form as well.
In the completion handler for register*Representation()
, i'm typically creating a temporary file and returning that.. (either the URL to the file or the data directly, depending on the API):
let tempFileUrl = URL.temporaryDirectory.appending(path: "testfile.png")
_ = tempFileUrl.startAccessingSecurityScopedResource()
defer { tempFileUrl.stopAccessingSecurityScopedResource() }
completion(tempFileUrl as data, nil) // option 1
completion(data, nil) // option 2
Disabling App Sandbox in the entitlements file does not work, but the error message goes away. So it seems like I have two problems: some sort of entitlement error and I must be using NSItemProvider()
incorrectly.
Anyone have any suggestions? I don't see to many examples out there for supporting exporting files (e.g. images) from an app into Finder.
Thanks!