{
"$type": "site.standard.document",
"description": "There are 2 types of drag and drop I'm interested in for SimplyDiskSweeper: Drag and drop folders onto the app icon in the Dock Drag and drop folders",
"path": "/add-support-for-drag-and-drop-to-swiftui-based-macos-app/",
"publishedAt": "2024-03-13T07:51:00.000Z",
"site": "at://did:plc:bryys25pc2fnagnyxqgsglhd/site.standard.publication/3mn26bjkkmh23",
"tags": [
"Swift",
"macOS",
"macOS Development"
],
"textContent": "There are 2 types of drag and drop I'm interested in for SimplyDiskSweeper:\n\n * Drag and drop folders onto the app icon in the Dock\n * Drag and drop folders onto the app window\n\nIt's easy to implement both in SwiftUI.\n\nDRAG AND DROP FOLDERS ONTO THE APP ICON IN THE DOCK\n\nContentView()\n .onOpenURL { url in\n handleDropOnDockAppIcon(url: url)\n }\n\nIf you use XcodeGen, add this to project.yml:\n\ntargets:\n <your-target-name>:\n #`settings`, `dependencies`, `scheme`, `entitlements` etc\n info:\n path: <your-dir-name>/Info.plist\n properties:\n CFBundleDocumentTypes:\n - CFBundleTypeRole: Viewer\n LSHandlerRank: Default\n LSItemContentTypes:\n - public.folder\n\notherwise amend Info.plist accordingly.\n\nDRAG AND DROP FOLDERS ONTO THE APP WINDOW\n\nContentView()\n .onDrop(of: [\"public.file-url\"], isTargeted: nil) { providers -> Bool in\n providers.first?.loadDataRepresentation(forTypeIdentifier: \"public.file-url\", completionHandler: { data, _ in\n if let data = data, let path = String(data: data, encoding: .utf8), let url = URL(string: path as String) {\n handleDropInWindow(url: url)\n }\n })\n return true\n }",
"title": "Add Support for Drag and Drop to SwiftUI-based macOS App"
}