Skip to content

NSItemProvider ​

NSItemProvider represents a provider for one item of content. The same item may come in several formats (HEIC and JPEG of the same photo), and the provider retrieves it in the type you ask for.

Each item selected in the photo picker comes with a provider, on the itemProvider of PHPickerResult. Currently only the members needed to get images are bound, and only UIImage is recognized.

You need to install @nacrejs/photos first; for how, see PHPickerViewController, or see Core and capability packages.

Inheritance: NSObject → NSItemProvider

Constructor ​

NSItemProvider()

Creates a provider from a UIImage. Picking photos doesn't need it: each selected item comes with its own provider.

Instance properties ​

PropertyDescription
registeredTypeIdentifiers read-onlyWhich formats the item has
suggestedName read-onlyThe file name the system suggests

Instance methods ​

MethodDescription
hasItemConformingToTypeIdentifier()Whether the item is a certain kind of content
canLoadObjectOfClass()Whether an object of a certain class can be retrieved
loadObjectOfClass()Retrieves an object of that class; returns a Promise

Uniform type identifiers ​

Uniform type identifiers are strings the system uses to represent data types, such as 'public.jpeg', 'public.heic', 'public.png', and 'com.apple.quicktime-movie'. They form a hierarchy: 'public.image' covers all image formats, and 'public.movie' covers all video formats. registeredTypeIdentifiers gives specific formats, while hasItemConformingToTypeIdentifier() can check against a parent name.

Examples ​

js
const picker = new PHPickerViewController(new PHPickerConfiguration());
picker.addEventListener('pick', async (event) => {
  const { results } = /** @type {PHPickerViewControllerFinishEvent} */ (event);
  for (const result of results) {
    const provider = result.itemProvider;
    if (provider.canLoadObjectOfClass(UIImage)) {
      const image = await provider.loadObjectOfClass(UIImage);
      console.log('Image ' + provider.suggestedName, image.size.width);
    } else if (provider.hasItemConformingToTypeIdentifier('public.movie')) {
      console.log('Video ' + provider.suggestedName + ', which cannot be retrieved yet');
    }
  }
});

Effect (not verified in the simulator): prints one line per selected item; images include the width, videos only the name.

Corresponding iOS API ​

ObjCJS
[[NSItemProvider alloc] initWithObject:image]new NSItemProvider(image)
[provider canLoadObjectOfClass:UIImage.class]provider.canLoadObjectOfClass(UIImage)
[provider loadObjectOfClass:UIImage.class completionHandler:^(id image, NSError *error) { … }]await provider.loadObjectOfClass(UIImage)

See also ​