All about Item Providers
The NSItemProvider
class in Foundation is a powerful abstraction for making data available across processes that are otherwise isolated from one another. I hope this post can be a one-stop reference for developers who want a solid understanding how item providers work, and how to use the API in a modern way.
This section provides a history of NSItemProvider
and its API surfaces. Feel free to skip it if you’re not interested.
NSItemProvider
was introduced in iOS 8.0 to support passing data between an application and extensions. The interface introduced at that time relied heavily on runtime Objective-C introspection, and every extension point specified its own peculiar way in which apps had to provide and receive data.
When Drag and Drop for iOS was introduced in iOS 11.0, a new set of API was introduced on the class to meet new requirements: Swift made it impractical to use Objective-C introspection, and Drag and Drop needed a more consistent and generalized abstraction, since the source and destination processes could be any app or extension.
The introduction of UniformTypeIdentifiers
in iOS 16.0 added a further extension to the class which uses UTType
instead of String
-typed identifiers for data type specifications. Methods to bridge between the Swift Transferable
protocol and NSItemProvider
were also introduced.
The existence of many historical layers in the NSItemProvider
API makes it confusing for the contemporary developer to understand exactly what they need to use in their code. It is my hope that this post can help steer developers toward the best set of API to use in their apps.
NSItemProvider
is a data promise
An NSItemProvider
is a promise to provide data. A provider constructs an NSItemProvider
object and registers available data. It then passes the item provider to some system API which will make it available to some consumer which will load the data. The provider needs not provide any data up front—data is requested only when a consumer attempts to load it.