I'm trying to achieve drag and drop on macOS with a custom type identifier to avoid collisions but it doesn't seem to work. First, here's a working example with a public and known identifier:
struct ReleaseView: View {
let id: Int
var body: some View {
GeometryReader { _ in
VStack(spacing: 16) {
Image(nsImage: NSImage(named: NSImage.networkName)!)
.contentShape(Rectangle())
.onDrag {
return NSItemProvider(item: "\(self.id)" as NSString, typeIdentifier: NSPasteboard.PasteboardType.string.rawValue)
}
DropZone()
}
}
}
}
struct DropZone: View {
@State var isDragging = false
var body: some View {
RoundedRectangle(cornerRadius: 16)
.stroke(style: StrokeStyle(lineWidth: 4, dash: [8, 8]))
.background(isDragging ? Color.secondary : Color.clear)
.frame(width: 100, height: 100)
.onDrop(of: [NSPasteboard.PasteboardType.string.rawValue], isTargeted: self.$isDragging) { itemProvider in
print(itemProvider)
return true
}
}
}
In this example, you can drag the image above into the drop zone and it will print out the provider. Now, merely changing the typeIdentifier
breaks everything.
static let sharedTypeIdentifier = "com.procrastin8.plzwork"
struct ReleaseView: View {
// skipping the unchanged bits
.onDrag {
return NSItemProvider(item: "\(self.id)" as NSString, typeIdentifier: sharedTypeIdentifier)
}
}
struct DropZone: View {
// skipping the unchanged bits
.onDrop(of: [sharedTypeIdentifier], isTargeted: self.$isDragging) { itemProvider in
print(itemProvider)
return true
}
}
Now this doesn't work. Using the same constant here, so it's not a string mismatch. Just yet another SwiftUI bug?