Search code examples
iosswiftdrag-and-dropuikit

UIDropInteractionDelegate performDrop not called?


I am trying to build a simple test app to learn Drag and Drop APIs. For this question I am focusing only on the Drop scenario. I have a blank View Controller, with the safari app open (multitasking) I then try to drag an image from google onto the View Controller's view.

I can drag the image from safari to my app's View Controller, but when I let go, this call below is never called:

func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession)

This is my code:

class EditTestVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        view.addInteraction(UIDropInteraction(delegate: self))
    }

}

extension EditTestVC:UIDropInteractionDelegate {

    func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool { // 1
        print("canHandle session: \(session)")
        return true
//        return session.canLoadObjects(ofClass: UIImage.self)
    }

    func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal { // 2
        print("sessionDidUpdate session: \(session)")
        return UIDropProposal(operation: .copy)
    }

    func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
        print("performDrop session: \(session)")
    }


}

The top two methods are called, this one:

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

and this one:

func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal

So, I am trying to figure out why this one:

func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession)

is never called.

Any suggestion?


Solution

  • I just tested your code and all methods are getting called. It must be a glitch.

    Troubleshooting steps:

    Delete app -> Restart the simulator -> Clean build folder -> Rebuild -> Run. You can also try running in different simulator first to save time.