I'm currently building a simple test app to play with drag and drop on macOS. I followed a tutorial to get a simple working code, and while it's working fine on iOS, if I run the exact same thing on macOS it doesn't anymore.
struct ContentView: View {
@State var backgroundColor: Color = .black
let colors: [String] = ["Yellow", "Green", "Pink"]
var body: some View {
VStack {
HStack {
ForEach(self.colors, id: \.self) { color in
Text(color)
.font(.title)
.onDrag {
NSItemProvider(object: color as NSString)
}
}
}
HStack {
RoundedRectangle(cornerRadius: 10)
.fill(backgroundColor)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.onDrop(of: ["public.text"], delegate: MyDropDelegate(color: $backgroundColor))
}
}
}
}
struct MyDropDelegate: DropDelegate {
@Binding var color: Color
func performDrop(info: DropInfo) -> Bool {
if let item = info.itemProviders(for: ["public.text"]).first {
item.loadItem(forTypeIdentifier: "public.text", options: nil) { (text, error) in
if let data = text as? Data {
let inputStr = String(decoding: data, as: UTF8.self)
if inputStr == "Yellow" {
self.color = .yellow
} else if inputStr == "Green" {
self.color = .green
} else if inputStr == "Pink" {
self.color = .pink
} else {
self.color = .gray
}
} else {
print(error)
}
}
} else {
return false
}
return true
}
}
On iOS, if I drag the color name on the rectangle it changes color as intended.
On macOS, however, the rectangle doesn't change and instead I get an Error Domain=NSItemProviderErrorDomain Code=-1000 "Cannot load representation of type public.text". If I drag the text into Safari for example, the text is copied fine there so the drag is not the problem, it's the drop part that I struggle with.
Is this a bug or intended behavior, or perhaps am I missing something ?