Adopt drop interaction APIs to selectively consume dragged content.
- iOS 11.0+
By implementing a drop interaction delegate (
UIDrop) for a view, you enable that view to accept dropped items.
Enable a View as a Drop Destination
Any instance or subclass of
UIView can act as a drop destination. Your first steps to make this happen are:
Create a drop interaction (a
Specify the drop interaction’s delegate (an object that conforms to the
Add the interaction to the view’s
Here’s how to do this:
Consider Accepting the Drag Items
When the user moves the touch point of a drag session over a drop destination, you can immediately refuse it, or you can tell the system to continue its conversation with your delegate object. Provide your response as follows:
This example assumes that your drop destination can consume only
UIImage objects. The method implementation tests for this type in the
return statement. You can also use this method to refuse a drop session based on the state of your app. The drop session is a system-managed object that conforms to the
UIDrop protocol. You can access a drop session for information about the items being dropped.
drop method is your app’s only chance to respond to the system’s question about whether your app will consider accepting the items. For example, you might preemptively reject a drop session if it contains no data representations your app can consume, or if consuming dragged items is inappropriate given your app state. Your app’s definitive opportunity to accept or reject dropped items is in your implementation of the
drop protocol method.
Provide the Required Drop Proposal
For a view to be eligible to accept the data from a drop session, you must implement the
drop protocol method. In your implementation, return a drop proposal—a
UIDrop object that specifies the drop operation type, a constant from the
Provide a drop proposal like this:
If your app instead opts to refuse the dropped items, return the
The system calls this protocol method when the user has moved the touch point of a drag session over a drop-enabled view—as long as the view did not already reject the drop by returning
false in its
drop method (see Consider Accepting the Drag Items).
Consume the Data in the Drag Items
The final step of the conversation between the drop interaction delegate and the system is when your app consumes the data the user has dragged from the source app. Here, your drop interaction delegate asks the drop session to load its drag items:
drop method is a destination app’s only opportunity to request representations of drag items. Receiving the items is potentially time-consuming and proceeds asynchronously. Do not wait within this method to receive items; instead, return from this method quickly.
Understand a Drop Destination in Context
When the touch point for a drop session moves over a view that you’ve configured as a drop destination, the system initiates a conversation with the drop interaction delegate. This conversation gives your app opportunities to accept or reject the drop, to prepare for consuming the drag items, and to update your model and UI, as shown here:
The figure above depicts the steps for consuming a drag item, in context:
The user moves their finger onscreen so the touch point of a drag session is within a configured view in your app. The system instantiates a drop session (an object that conforms to the
UIDropprotocol, not shown in the figure) for managing the drop activity.
The system calls the drop interaction delegate’s
dropprotocol method. Check whether your app can, and opts to, consume the drag items.
Interaction(_: can Handle:)
The system calls the delegate’s
dropprotocol method. Prepare to consume the drag items.
Interaction(_: session Did Enter:)
The system calls the delegate’s
dropprotocol method. Your implementation must return a UIDropProposal object, or the system ends the session.
Interaction(_: session Did Update:)
If the user confirms their intent to complete the drop, the system calls the delegate’s asynchronous
dropprotocol method. This a destination app’s only opportunity to request representations of drag items.
Interaction(_: perform Drop:)