- iOS 11.0+
- Xcode 11.0+
With drag and drop, users can copy data from one iPad app to another. The data is shared between the apps using an
NSItem object. This sample code project shows how to use an item provider to:
Share a contact that the user drags from the sample iPad app and drops into another app.
Retrieve contact information that the user drags from another app and drops into the sample app.
The sample iPad app uses a table view to display a list of contacts. The user can drag one or more contacts and drop them into another app such as Contacts or Notes. Also, the user can use another app, like Contacts or Notes, to drag and drop new contacts into the sample app.
Add Drag Support
The table view in the sample app displays a list of contacts. When the user begins dragging a contact, the system calls the
table method. The method’s implementation retrieves the contact from the list using the
at parameter to determine the contact’s location within the list. The method then creates an item provider with the contact object and wraps the item provider in a
UIDrag object. The method places the drag item into an array which it returns to the system. Finally, the system adds the drag item to the current drag session.
The user may select multiple contacts to drag to another app. Each time the user selects a contact, the system calls the
table method. Similar to beginning a drag session, this method creates an item provider for the selected contact, creates a drag item that contains the item provider, and returns an array containing the drag item so the system can add the drag item to the drag session.
In both cases, the
UITable methods create item providers using instances of the
Contact class. This class implements the
NSItem protocol, making it possible to initialize a new item provider with a contact card object.
By conforming to this protocol, a contact card object can tell the item provider the data types it supports; the sample app supports vCard and plain text. A contact card can also load data of a specified type, which an item provider retrieves by calling the contact card’s
Add Drop Support
When the user drops contact information from another app into the sample app, the system calls the
Contacts implements this method to handle the drop operation. The implementation of the method uses the drop item’s item provider to call
load, which retrieves a
Contact object representing the dropped contact information.
load method asks the
Contact class for the contact card object. The class, which conforms to the
NSItem protocol, implements the
object(with class method, which creates and initializes a contact card object with the item provider data.
When the user drops a contact at a specific location within the table view, the completion handler (from the
load call) creates a placeholder that displays a gap at the drop location. Next, the handler inserts the dropped contact into the list of contacts at the index path of the drop location. And finally, the completion handler replaces the placeholder with a view displaying contact.
When the user drops the contact on an empty location in the table view, the completion handler adds the dropped contact to the end of the list without displaying a gap.