Drag&Drop Custom Types with Transferable Protocol in Swiftui for macOS
Asked Answered
S

0

5

I'm trying to implement a simple drag and drop feature with the transferable protocol. In my application, I have a Task struct as follows:

struct Task: Identifiable, Hashable, Equatable, Codable, Transferable{
    var id = UUID()
    var description: String
    var status: Int = 0
    var tags: [Tag] = []
    var inKanBan: Bool = false
    var archived: Bool = false
    

    init(description: String){
        self.description = description
    }
  
    static var transferRepresentation: some TransferRepresentation {
        CodableRepresentation(contentType: .task)
    }
}

extension UTType {
    static let task: UTType = UTType(exportedAs: "some.app.Task")
}

And my test view is as follows:

var body: some View {
        HStack {
            
            VStack{
                ForEach(taskManager.getKanBanToDoList(), id:\.self){ task in
                    RoundedRectangle(cornerRadius: 5)
                        .fill(.blue)
                        .frame(width:50, height: 20)
                        .foregroundColor(.white)
                        .overlay{
                            Text(task.description)
                        }
                        .draggable(task)
                }
            }
            Color.pink
                .frame(width: 400, height: 400)
                .opacity(0.5)
                .dropDestination(for: Task.self) { items, location in
                    print("dropping \(items.first!.id)")
                    return true
                } isTargeted: { inDrop in
                    print("targeted..")
                }
            
        }
    }

I've also added the "Exported Type Identifiers".

The problem is, this code is working when I set Supported Destination as Mac Catalyst. However, it is not working if the destination is macOS. As far as I know, Transferable protocol is working in macOS too. When I change the code for Text views, the code is working. But not for the custom types. What would be the problem? Am i missing something?

Selfinsurance answered 25/8, 2023 at 12:18 Comment(3)
I have the same issue. I haven't tried Catalyst, my app has iOS and macOS targets. I'm using the RC of both OSes. Here's what I tried: - using known Transferable type (like String) work on iOS and macOS. - Using a custom Transferable type (in my case PersistentIdentifier) works on iOS. - The same custom Transferable types do not work on macOS. .dropDestination will not accept it being dropped (No green + sign, and isTargeted is not called).Agitator
September 2024, MacOS Sonoma 14.5, Xcode 16. I cannot test this on Catalyst because it demands a higher version of MacOS and does not let me set a lower target; but on MacOS the dragging works fine and the .dropDestination never fires (I had exactly the same problem); on iOS, the dragging fails. serialcoder.dev/text-tutorials/swiftui/… has the same problem; dragging never becomes enabled.Theriot
I've posted the answer here https://mcmap.net/q/1997457/-dropdestination-doesn-39-t-execute-with-my-custom-transferable-type - when you add the type identifier to the target, it autocreates an entry for 'icons'; delete that, and drag and drop works.Theriot

© 2022 - 2025 — McMap. All rights reserved.