6

I am currently developing an iOS application containing a Share extension.

I realized that the NSExtensionActivationSupportsImageWithMaxCount key doesn't allow me to activate my Share extension on .jpeg or .png URLs ("public.image" UTI, kUTTypeImage) under Safari (ie : an imgur link).

I can still activate and test my extension if I switch to a NSActivationRule = TRUEPREDICATE, but it is forbidden for a released app.

I filled a bug on radar in case of it wasn't wanted (even facebook, twitter, etc... aren't activated on this URLs)

Right now, I would like to combine the following keys and the "public.image" in a NSPredicate string as the documentation says (https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW8)

So I have to translate the keys to a UTI

So far I have translated:
- NSExtensionActivationSupportsFileWithMaxCount to "public.file-url" kUTTTypeFileURL
- NSExtensionActivationSupportsMovieWithMaxCount to "public.movie" kUTTypeMovie
- NSExtensionActivationSupportsText to "public.text" kUTTypeText
- NSExtensionActivationSupportsWebURLWithMaxCount to "public.url" kUTTypeURL

But I don't find the type translation for:

  • NSExtensionActivationSupportsWebPageWithMaxCount, "public.HTML" is it kUTTypeHTML ?

Does somebody already used this keys inside a predicate?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Power78
  • 311
  • 4
  • 12

1 Answers1

17

What I ended up doing is 1) allowing TRUEPREDICATE temporarily, and using some logic like this`

    NSExtensionItem *item = extensionContext.inputItems.firstObject;

    if ( item )
    {
        NSItemProvider *itemProvider = item.attachments.firstObject;

        if ( itemProvider )
        {
            NSArray *registeredTypeIdentifiers = itemProvider.registeredTypeIdentifiers;
            NSLog( @"registeredTypeIdentifiers: %@", registeredTypeIdentifiers );
        }
     }`

This will give you all the types of the document you want to share (example: "public.url"). Because of the multiple types, my predicate became a little complex:

SUBQUERY (
                extensionItems,
                $extensionItem,
                SUBQUERY (
                $extensionItem.attachments,
                $attachment,

                (
                           ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.adobe.pdf"
                        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.image"
                        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.plain-text"
                        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.png"
                        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg"
                        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg-2000"
                        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.tiff"
                        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.compuserve.gif"
                        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.microsoft.bmp"
                        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.microsoft.word.doc"
                        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "org.openxmlformats.wordprocessingml.document"
                        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.comma-separated-values-text"
                )
                AND
                (
                     NOT ( ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.adobe.photoshop-image" )
                )

    ).@count == $extensionItem.attachments.@count
).@count == 1

This basically looks for any file type for image (other than adobe psd), pdf, txt, csv or doc/docx. It also only allows 1 document to be shared at a time.

It looks like kUTTypeImage includes PSD - hence my blocking of that format ( "com.adobe.photoshop-image" ).

Herbal7ea
  • 306
  • 3
  • 8
  • 3
    When writing your own predicate strings, watch out for the `"` be sure **not** to use `”` by mistake. You would be amazed at the amount of time you can lose with these kinds of issues. – Shumais Ul Haq Mar 10 '16 at 12:26
  • Good catch, Shumais UI Haq. That has gotten me so many times. It usually happens when I cut and paste from a Word Document. – Herbal7ea Mar 16 '16 at 16:39
  • 1
    What if we just use `public.data` in this predicate instead of having specific content types? – D4ttatraya Dec 24 '18 at 15:11
  • 1
    To be exact, this doesn't guarantee you that there is max 1 attachment. Your share extension would be offered also for multi-selection in gallery. E.g. you can read more here: https://medium.com/@cmoulinet/ios-share-extension-custom-rules-to-limit-type-or-numbers-of-medias-selected-91ab596d505 – Marek Manduch Jan 20 '20 at 16:27
  • @D4ttatraya I just tried with `public.data`, it does accept all files. This type saved me from providing explicit types. – Honghao Z Nov 16 '20 at 06:16
  • 1
    Something to watch out for which fooled me, if a Numbers document has multiple sheets, CSV exports are not identified as "public.comma-separated-values-text", but as "public.zip-archive", "public.file-url" and are zipped – John Popham Jan 14 '21 at 00:35