Stuart Breckenridge

The Drag and Drop API Is Simplicity at Its Best

Sometimes an API comes out of nowhere and astounds you with its power and simplicity. I’ve just had that experience with the new drag and drop API in iOS 11. In my FFI List app, with around 20 lines of code, I’ve been able to implement functionality that allows users to drag an FFI from the app into any other app that supports text drops.

The first step is specifying a drag delegate for the table view:

savedFFITableView.dragDelegate = self

The second step is implementing the UITableViewDragDelegate:

func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
    return dragItems(for: indexPath)

The final step, similar to Apple’s sample code, is to create the drag item(s):

func dragItems(for indexPath: IndexPath) -> [UIDragItem] {
    let item = CDStack.shared.savedFetchedResultsController.object(at: indexPath) as! FFISavedEntity
    var validity:String {
        if item.valid == "false" {
            return "This FFI is currently not on the GIIN list."
        } else {
            return "This FFI is currently on the GIIN list."
    let string = "Name: \(\nGIIN: \(item.giin)\n\(validity)".data(using: .utf8)
    let itemProvider = NSItemProvider()
    itemProvider.registerDataRepresentation(forTypeIdentifier: kUTTypePlainText as String, visibility: .all) { completion in
        completion(string, nil)
        return nil
    return [UIDragItem(itemProvider: itemProvider)]

With those three short snippets this is the result:

What I want to do — and what I haven’t worked out yet — is dragging from the search table view on to the saved tab in order to save FFIs. It’ll take a bit more work, but I’m sure it’s doable.

— Supported by —