How to drag custom items in SwiftUI on macOS

How can we drag views coupled to an NSItemProvider with a custom typeIdentifier or UTI ?
I know you can use the following methods in conjunction with predefined UTI types (like kUTTypeText etc.):But what if I want to use an itemProvider with a custom UTI?
I tried:
Code Block swift
import SwiftUI
struct ContentView: View {
static let type = "com.devian.sampleapp.customstring.dragdroptype"
@State var dropZoneIsHovered = false
var body: some View {
VStack {
Text("Hello")
.padding()
.onDrag(createItem)
Text("Dropzone")
.padding()
.background(dropZoneIsHovered ? Color.gray : nil)
.onDrop(
of: [ContentView.type],
isTargeted: $dropZoneIsHovered,
perform: acceptDrop(items:)
)
}.padding()
    }
func createItem() -> NSItemProvider {
NSItemProvider(
item: "Hello" as NSString,
typeIdentifier: ContentView.type
)
}
func acceptDrop(items: [NSItemProvider]) -> Bool {
print("Hurray we received a custom item")
return true
}
}

But the dropzone does not accept custom UTI's
I also tried to export the custom UTI using Info.plist:
Code Block xml
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
<key>UTTypeIdentifier</key>
<string>com.devian.sampleapp.customstring.dragdroptype</string>
<key>UTTypeTagSpecification</key>
<dict/>
</dict>
</array>

But that did not work either. Then I tried to add it as a document type in Info.plist

Code Block xml
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>None</string>
<key>LSItemContentTypes</key>
<array>
<string>com.devian.sampleapp.customstring.dragdroptype</string>
</array>
<key>LSTypeIsPackage</key>
<integer>0</integer>
</dict>
</array>


Nothing of the above works on macOS. What am I doing wrong?

I made just a couple of changes to the plist to get your sample to work:

For the plist, my new fragment looks like this:

<key>UTExportedTypeDeclarations</key>
  <array>
    <dict>
      <key>UTTypeConformsTo</key>
      <array>
        <string>public.data</string>
      </array>
      <key>UTTypeIdentifier</key>
      <string>com.devian.sampleapp.customstring.dragdroptype</string>
    </dict>
  </array>

And that's it, your code unmodified works now.

How to drag custom items in SwiftUI on macOS
 
 
Q