KevinF
KevinF

Reputation: 111

SwiftUI DropDestination Using Custom Transferable Type and Using UTI / UTType Not Working

enter image description here

I am trying to implement drag and drop using the Transferable protocol within my app. I followed the below tutorial but can't get it to work. I created this very simple example and it still doesn't work. I must be missing something simple now. https://serialcoder.dev/text-tutorials/swiftui/first-experience-with-transferable-implementing-drag-and-drop-in-swiftui/

import SwiftUI
import Foundation
import UniformTypeIdentifiers

struct ContentView: View {

    @State private var draggedOutput: String = ""
    var body: some View {
    VStack(spacing: 50) {
        Text("Text")
            .font(.title)
            .draggable("DRAGGED TEXT")
        Text("Custom Transferable")
            .font(.title)
            .draggable(CustomTransferable())
        Divider()
        Text(draggedOutput)
            .font(.largeTitle)
            .frame(width: 250, height: 250)
            .background(Color.green)
            .dropDestination(for: String.self) { items, location in
                draggedOutput = items.first!
                return true
            }
            .dropDestination(for: CustomTransferable.self) { items, location in
                draggedOutput = items.first!.id.uuidString
                return true
            }
    }
    .padding()
}
}


struct CustomTransferable: Identifiable, Codable, Transferable {

    static var transferRepresentation: some TransferRepresentation {
    CodableRepresentation(for: CustomTransferable.self, contentType: UTType(exportedAs: "com.gmail.Me.D.ExtraInfoAudio"))
}

let id: UUID

init(id: UUID = UUID()) {
    self.id = id
}
}

enter image description here

Upvotes: 2

Views: 1016

Answers (1)

KevinF
KevinF

Reputation: 111

I switched the order of the dropDestination modifiers and my custom drop started to work and the standard String one stopped working. I don't see it in the documentation or the WWDC talk for Transferable but it seems that only 1 dropDestination works

Upvotes: -1

Related Questions