Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an iOS Share/Action Extension to show up only for a single image

I have an action extension in an iOS app that I only want to be available when the user is sharing a single image. The NSExtension key in my info.plist looks like this.

<key>NSExtension</key>
<dict>
    <key>NSExtensionAttributes</key>
    <dict>
        <key>NSExtensionActivationRule</key>
        <dict>
            <key>NSExtensionActivationSupportsImagesWithMaxCount</key>
            <integer>1</integer>
        </dict>
    </dict>
    <key>NSExtensionMainStoryboard</key>
    <string>MainInterface</string>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.ui-services</string>
</dict>

The only activation rule I am using is NSExtensionActivationSupportsImagesWithMaxCount with a value of 1. However, the extension still shows up when sharing other things. For example, it shows up when I hit the action button in Safari.

In the Safari case, there is no image to be pulled out of the NSExtensionContext.

Anyone have any idea how to get my extension to not show up in those cases?

like image 633
Ross Kimes Avatar asked Nov 23 '16 16:11

Ross Kimes


2 Answers

Looks like a predicate could fix it:

https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html

Action Extension activation rule predicate doesn't hide the action when multiple of the same type is selected

How do I set NSExtensionActivationRule predicates?

like image 138
mattyb Avatar answered Nov 14 '22 18:11

mattyb


Was able to get this working thanks to @Matthew-Bordas. The solution was to use a predicate.

<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;public.image&quot; ).@count == 1 ).@count == 1</string>
  </dict>
  <key>NSExtensionMainStoryboard</key>
  <string>MainInterface</string>
  <key>NSExtensionPointIdentifier</key>
  <string>com.apple.ui-services</string>
</dict>
like image 2
Ross Kimes Avatar answered Nov 14 '22 19:11

Ross Kimes