Issue with .itemProvider on macOS 15.1

I have a List with draggable items. According to this thread (https://developer.apple.com/forums/thread/664469) I had to use .itemProvider instead of .onDrag, because otherwise the selection of the list will not work anymore.

The items in my list refer to a file URL. So the dragging allowed to copy the files to the destination of the drag & drop. Therefore I used this code

.itemProvider {
    let url = ....... // get the url with an internal function
    return NSItemProvider(object: url as NSURL)
}

Since the update to macOS 15.1 this way isn't working anymore. It just happens nothing.

I also tried to use

.itemProvider {
	let url = ....
	return NSItemProvider(contentsOf: url) ?? NSItemProvider(object: url as NSURL)
}

but this doesn't work too.

The same way with .onDrag works btw.

.onDrag {
    let url = ....... // get the url with an internal function
    return NSItemProvider(object: url as NSURL)
}

but as I wrote, this will break the possibility to select or to use the primaryAction of the .contextMenu.

Is this a bug? Or is my approach wrong and is there an alternative?

@joachim_me Have you tried using the draggable(_:) and dropDestination(for:action:isTargeted:) API's for drag and drop operation?

Adopting drag and drop using SwiftUI sample code would walk you through on how to implement drag and drop to receive Transferable items

Yes, I tried to use draggable, but it just happens nothing. But maybe I don't understand correctly how to implement it.

My model Document conforms to Codable:

List(selection: self.$selectedDocument) {
    ForEach(self.documents) { document in
        DocumentView(document)
            .draggable(document)
    }

and

extension Document: @retroactive Transferable {
	public static var transferRepresentation: some TransferRepresentation {
		FileRepresentation(exportedContentType: .data) { document in
			return SentTransferredFile(document.url)
		}
	}
}

Are you dragging the item within your app, between other apps you own or between your app and your app and apps in your system such as Finder, Files, Notes etc?

are you exporting a directory or URL to a file in the file system? If that's the case then use directory or fileURL content type.

Sorry, that I didn't add this information:

I am dragging from my app to another app like Finder or Mail. My original code with .itemProvider exported a NSURL, then Finder copied the file to the drop location and Mail used the file as an attachment. Using now .itemProvider it just happens nothing in Finder or Mail.

So what to use now from this? https://developer.apple.com/documentation/coretransferable/choosing-a-transfer-representation-for-a-model-type

I even tried this workaround: https://nonstrict.eu/blog/2023/transferable-drag-drop-fails-with-only-FileRepresentation/

Ah, I found out, that it seems to be a bug (the search was long!!).

I could implement the Transferable protocol successfully and I can drag and drop the file to the Finder. BUT: It's not working, if you put the view with the .draggable into a List (with ForEach).

Any ideas?

EDIT:

Here a working example:

struct Document: Transferable {
	let name: String
	let url: URL
	
	static var transferRepresentation: some TransferRepresentation {
		FileRepresentation(contentType: .data) { document in
			SentTransferredFile(document.url)
		} importing: { received in
			let tmpFileURL = FileManager.default.temporaryDirectory.appending(path: received.file.lastPathComponent)
			try FileManager.default.copyItem(at: received.file, to: tmpFileURL)
			return Self.init(name: "", url: tmpFileURL)
		}
		.suggestedFileName { document in
			document.url.lastPathComponent
		}
	}
}

struct ContentView: View {
	
	let document = Document(name: "Test-File", url: FileManager.default.homeDirectoryForCurrentUser.appending(path: "Desktop/test.txt"))
	
	var body: some View {
		VStack {
			Text("file")
				.draggable(document)
		}
		.padding()
	}
}

But this will not work:

var body: some View {
		VStack {
			List {
				Text("file")
					.draggable(document)
			}
		}
		.padding()
	}

I can see, that dragging works, but if I drop the file into the Finder, just nothing happens.

Issue with .itemProvider on macOS 15.1
 
 
Q