0

I am working with a simple SwiftUI List and I want to enable functionality that allows files and images to be dropped onto the List from outside the app. There is an onInsert modifier with List that allows for this, but I found that it works on macOS, but not when running the same on iOS.

Here is a simple example that reproduces the issue:

struct TestListInsertView: View {
    let sampleData: [String] =
        ["swift", "ios", "xcode", "swiftui"]
    
    var body: some View {
        List {
            ForEach(sampleData, id: \.self) { string in
                Text(string)
            }
            .onInsert(of: CJSwiftDragDropHandler.arrayForTypes(), perform: dropAction)
        }
    }
    
    private func dropAction(at index: Int, _ items: [NSItemProvider]) {
        print("FilesListView: List onInsert - calling dropAction with index \(index), with items.count = \(items.count)")
    }

}

#Preview {
    TestListInsertView()
}


@objc public class CJSwiftDragDropHandler: NSObject, ObservableObject {

    public static func arrayForTypes() -> [UTType] {
        return [UTType.fileURL, UTType.data, UTType.image, UTType.audio, UTType.movie, UTType.url]
    }
}

Run the same content view in MacOS and it highlights the 'drop' and the dropAction callback is triggered. But on iOS, there is no visual cue or highlight for the drop and nothing is triggered.

Is there anything I can do to make it work consistently?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.