Clicky

iOS Dev Nugget 160 NSExtensionActivationRule for App/Share Extensions and using NSPredicate

.

Need to run a code review on your codebase? Hire me

When including a share or action extension in your app, you can use the NSExtensionActivationRule key in your extension's Info.plist file to specify the data types your extension is available for. If you want it to support a maximum of 1 image:

<key>NSExtension</key>
<dict>
    ...
    <key>NSExtensionAttributes</key>
    <dict>
        <key>NSExtensionActivationRule</key>
        <dict>
            <key>NSExtensionActivationSupportsImageWithMaxCount</key>
            <integer>1</integer>
        </dict>
    </dict>
    ...
</dict>

Sometimes, your filtering criteria might need to be more granular. You can use a NSPredicate string as the value of NSExtensionActivationRule instead. E.g. if we want to specific that we only want Live Photos (instead of all kinds of images), and we want exactly 1 live photo:

<key>NSExtension</key>
<dict>
    ...
    <key>NSExtensionAttributes</key>
    <dict>
        <key>NSExtensionActivationRule</key>
            <string>SUBQUERY (
          extensionItems,
          $extensionItem,
          SUBQUERY (
              $extensionItem.attachments,
              $attachment,
              ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO &quot;com.apple.live-photo&quot;
          ).@count == $extensionItem.attachments.@count and $extensionItem.attachments.@count == 1
        ).@count == 1</string>
    </dict>
    ...
</dict>


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues