NSItemProvider
An item provider for conveying data or a file between processes during drag-and-drop or copy-and-paste activities, or from a host app to an app extension.
Declaration
class NSItemProviderOverview
Starting in iOS 11, item providers play a central role in drag and drop, and in copy and paste. They continue to play a role with app extensions.
The system uses an internal queue when calling the completion blocks for the NSItemProvider class. When using an item provider with drag and drop, ensure that UI updates take place on the main queue as follows:
DispatchQueue.main.async {
// Work that impacts the user interface.
}App extension support
An app extension typically encounters item providers when examining the attachments property of an NSExtensionItem object. During that examination, the extension can use the hasItemConformingToTypeIdentifier(_:) method to look for data that it recognizes. Item providers use Uniform Type Identifiers values to identify the data they contain. After finding a type of data that your extension can use, it calls the loadItem(forTypeIdentifier:options:completionHandler:) method to load the actual data, which is delivered to the provided completion handler.
You can create item providers to vend data to another process. An extension that modifies an original data item can create a new NSItemProvider object to send back to the host app. When creating data items, you specify your data object and the type of that object. You can optionally use the previewImageHandler property to generate a preview image for your data.
A single item provider may use custom blocks to provide its data in many different formats. When configuring an item provider, use the registerItem(forTypeIdentifier:loadHandler:) method to register your blocks and the formats each one supports. When a client requests data in a particular format, the item provider executes the corresponding block, which is then responsible for coercing the data to the appropriate type and returning it to the client.
Topics
Creating an item provider
init(contentsOf:)init(contentsOf:contentType:openInPlace:coordinated:visibility:)init(item:typeIdentifier:)init()init(object:)
Configuring the provider
preferredPresentationSizepreferredPresentationStyleNSItemProvider.PreferredPresentationStylesuggestedNameteamData
Querying the provider’s contents
canLoadObject(ofClass:)canLoadObject(ofClass:)hasItemConformingToTypeIdentifier(_:)hasRepresentationConforming(toTypeIdentifier:fileOptions:)registeredTypeIdentifiersregisteredTypeIdentifiers(fileOptions:)
Loading the provider’s contents
loadItem(forTypeIdentifier:options:completionHandler:)loadDataRepresentation(forTypeIdentifier:completionHandler:)loadDataRepresentation(for:completionHandler:)loadFileRepresentation(forTypeIdentifier:completionHandler:)loadFileRepresentation(for:openInPlace:completionHandler:)loadInPlaceFileRepresentation(forTypeIdentifier:completionHandler:)loadObject(ofClass:completionHandler:)loadObject(ofClass:completionHandler:)loadTransferable(type:completionHandler:)
Loading a preview image
Registering CloudKit shares
registerCloudKitShare(_:container:)registerCloudKitShare(preparationHandler:)registerCKShare(_:container:allowedSharingOptions:)registerCKShare(container:allowedSharingOptions:preparationHandler:)
Registering content types
Registering data
registerDataRepresentation(forTypeIdentifier:visibility:loadHandler:)registerDataRepresentation(for:visibility:loadHandler:)registerItem(forTypeIdentifier:loadHandler:)
Registering files
registerFileRepresentation(forTypeIdentifier:fileOptions:visibility:loadHandler:)registerFileRepresentation(for:visibility:openInPlace:loadHandler:)
Registering group activities
Registering objects
registerObject(_:visibility:)registerObject(ofClass:visibility:loadHandler:)registerObject(ofClass:visibility:loadHandler:)register(_:)
Getting the provider’s frame
Constants
NSItemProvider.CompletionHandlerNSItemProvider.LoadHandlerOptions Dictionary KeyKeys for Items Accessed in JavaScript CodeerrorDomainNSItemProviderFileOptionsNSItemProviderReadingNSItemProviderWritingNSItemProviderRepresentationVisibilityNSItemProvider.ErrorCode