Skip to content

Instantly share code, notes, and snippets.

@steipete
Created April 4, 2021 13:43
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save steipete/f61bb38ddee07a691bff8a4f681d3486 to your computer and use it in GitHub Desktop.
Save steipete/f61bb38ddee07a691bff8a4f681d3486 to your computer and use it in GitHub Desktop.
Accept dropping a file onto SwiftUI (both iOS and macOS)
.onDrop(of: [.fileURL], isTargeted: nil) { providers in
if let loadableProvider = providers.first(where: { $0.canLoadObject(ofClass: URL.self) }) {
_ = loadableProvider.loadObject(ofClass: URL.self) { fileURL, _ in
if let fileURL = fileURL, fileURL.pathExtension.lowercased() == "zip" {
self.logger.info("Dropped \(fileURL.path)")
DispatchQueue.main.async {
importer.open(zipArchiveURL: fileURL)
}
}
}
return true
}
return false
}
@steipete
Copy link
Author

steipete commented Apr 5, 2021

MIT License.

@Lawris
Copy link

Lawris commented Feb 11, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment