Instance Method

dropInteraction(_:canHandle:)

Asks the delegate whether it can handle the session’s drag items.

Declaration

optional func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool

Parameters

interaction

The interaction that called this method.

session

The current drop session.

Return Value

true if the interaction supports the drag items in the sessions; otherwise, false.

Discussion

Returning true doesn't mean the interaction will accept the drop. Instead, it tells the system that the interaction has interest in, and can handle, the drop session.

To determine if the interaction can handle the session, check the data type of session's drag items. For instance, if you're interested in drop activities that contain images, you can do the following:

func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
    // Ensure the drop session has an object of the appropriate type
    return session.canLoadObjects(ofClass: UIImage.self)
}

You can also be more specific by using a uniform type identifier (UTI) for the data type. For instance, if you’re interested in only PNG images, use the UTI for PNG files:

import MobileCoreServices // for kUTTypePNG

func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
    return session.hasItemsConforming(toTypeIdentifiers: [kUTTypePNG as String])
}

See Also

Handling the Drop

func dropInteraction(UIDropInteraction, performDrop: UIDropSession)

Tells the delegate it can request the item provider data from the session’s drag items.