János
János

Reputation: 35114

Drag-and-Drop Works in iOS Simulator but Not in Mac App

I implemented drag-and-drop in a SwiftUI app. It works perfectly in the iOS simulator but doesn't function in the Mac Catalyst version. The drag gesture is recognized, but the drop action fails. What could be causing this issue in Catalyst?

struct ContentView: View {
    @State private var items = ["Item 1", "Item 2", "Item 3"]

    var body: some View {
        ScrollView {
            LazyVStack {
                ForEach(items, id: \.self) { item in
                    Text(item)
                        .padding()
                        .background(Color.blue)
                        .cornerRadius(8)
                        .foregroundColor(.white)
                        .onDrag {
                            NSItemProvider(object: item as NSString)
                        }
                }
            }
        }
        .onDrop(of: ["public.text"], delegate: CustomDropDelegate(items: $items))
    }
}

struct CustomDropDelegate: DropDelegate {
    @Binding var items: [String]

    func performDrop(info: DropInfo) -> Bool {
        guard let item = info.itemProviders(for: ["public.text"]).first else { return false }
        item.loadItem(forTypeIdentifier: "public.text", options: nil) { (data, error) in
            DispatchQueue.main.async {
                if let textData = data as? Data, let text = String(data: textData, encoding: .utf8) {
                    self.items.append(text)
                }
            }
        }
        return true
    }
}

Upvotes: 0

Views: 22

Answers (0)

Related Questions