Drag & Drop issues in SwiftUI in a Playground App

I am prototyping an iOS App in Swift Playgrounds. The App has Drag & Drop features internally using custom identifiers.

The data that is being moved around is a struct that contains a deeplink URL that can be used to identify my models.

I have three different entities to deal with. One is identified with a UTI com.example.item, the other two as com.example.collection. Items may be dropped onto Collections and visa versa, but the same type my not be dropped onto itself.

For onDrag(_:) I produce an NSItemProvider and register the entity using registerDataRepresentation(forTypeIdentifier: visibility: loadHandler:). The ‘loadHandler’ encodes the URL of the entity as Data. I use the appropriate custom UTI String for the entity.

So far so good, I can see that the right UTI String has been registered by examining the NSItemProvider.

For onDrop(of:delegate:) I provide the valid UTI (this filter does not seem to be honoured) and a custom class implementing DropDelegate, giving it a reference to the entity at the drop target.

As the filter does not seem to work, my DropDelegate implements func validateDrop(info: DropInfo) -> Bool.

In this function, I attempt to filter which drops are valid by calling info.hasItemConformingTo(_:) and checking against the drop target.

What I am finding is that even though I can see my custom UTI strings have been registered in the NSItemProviders, they are never visible when calling hasItemConformingTo(_:).

In this situation I would be unable to implement performDrop AFAICS.

I wonder if this problem is to do with my misunderstanding of how Drag & Drop works (I have also attempted other approaches like registering custom Objects with the NSItemProvider, but have the same problem.

Is this possibly caused by my environment? As I am writing this prototype in Swift Playgrounds, where there is no Info.plist, so I am unable to register custom UTI schemes?

Many thanks for any suggestions.

  • To test my hypothesis about the environment being the cause, I replaced my two custom UTIs with system UTIs (public.item and public.folder). The missing provider issues went away, my code was working but the system was not making the custom UTIs available to the DropDelegate.

    However, the issue I raised with the filter not working in onDrop(of:delegate:) still seems broken.

  • Hello, have you found a solution for this? I am trying to use string type identifier for drag and drop, but when dropping, the string is not visible to hasItemConforming. I am also on Swift Playgrounds app project.

Add a Comment