Dealing with extensionContext.inputItems for ShareExtension. Why only get firstObject of inputItems?

279 views Asked by At

Why, when dealing with Share Extensions and the extensionContext.inputItems inside of a view controller, do people (and the Apple docs) only get the firstObject of the inputItems instead of iterating over all of them?

2

There are 2 answers

0
Pedro José Cardoso On

Did you check all the attachments of that first inputItem?

I struggled with this for a while, but then I found my selections were set as attachments for that first input item.

Also, as Tom Harrington mentioned, confirm you have the right settings in NSExtensionActivationRule.

0
Tom Harrington On

Because,

  • Your NSExtensionActivationRule may indicate that only one item is permitted, e.g. NSExtensionActivationSupportsImageWithMaxCount has a value of 1.
  • Your extension may only be able to handle a single item, e.g. a single shared photo. This should only happen if the activation rule also restricts the extension to a single item. But if you screwed that up and you have 10 items, your code can still only deal with one.

In either case there's no point in iterating through the array. There should only be one item, and if for some reason more than one item is present, your code can't handle it anyway.