I have implemented function that returns NSItemProvider
func dragOutsideWnd(url: URL?) -> NSItemProvider {
if let url = url {
TheApp.appDelegate.hideMainWnd()
let provider = NSItemProvider(item: url as NSSecureCoding?, typeIdentifier: UTType.fileURL.identifier as String)
provider.suggestedName = url.lastPathComponent
//provider.copy()// This doesn't work :)
//DispatchQueue.main.async {
// TheApp.appDelegate.hideMainWnd()
//}
return provider
}
return NSItemProvider()
}
and I have use it this way:
.onDrag {
return dragOutsideWnd(url: itm.url)
}
This drag&drop action performs file MOVE action to any place of FINDER/HDD.
But how to perform COPY action?
Remember Drag&Drop is actually implemented with
NSPasteboard.I have written an example for you: GitHub
Now the key to your questions:
To control dragging behavior(your window is the source):
Draggable objects conform to the
NSDraggingSourceprotocol, so check the first method of the protocol:As the method docsuggests, return different
NSDragOperationin this delegation method. That includes: "Copy","Move", "Link", etc.To control dropping behavior(your window is the destination):
NSView that accepts drop conforms to the
NSDraggingDestinationprotocol, so you need to override the draggingEntered(_:) method by adding this code inside the DestinationView class implementation:More info form Apple's Documentation
For swiftUI, a simple show case SwiftUI Showcase
Further Reading: RayWenderlich.com has a detailed tutorial Drag and Drop Tutorial for macOS tutorial for you(needs a little swift upgrade).