Swift Share Extension not shown for PDFs

1.7k views Asked by At

Using Swift5.3.2, iOS13.0,

My Share Extension is working for images and videos.

However it does not work for PDFs.

The problem is that my App is not visible in the list of Share-Apps for a PDF document that I am trying to share with my App.

I know that the rules must be correctly set inside the info.plist.

I tried the following two attempts - but both without success !

Can anybody tell me what a PDF share extension needs in iOS ?

attempt 1: Info.plist

<key>NSExtension</key>
<dict>
    <key>NSExtensionAttributes</key>
    <dict>
        <key>NSExtensionActivationRule</key>
        <dict>
            <key>NSExtensionActivationSupportsFileWithMaxCount</key>
            <integer>20</integer>
            <key>NSExtensionActivationSupportsWebPageWithMaxCount</key>
            <integer>1</integer>
            <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
            <integer>1</integer>
            <key>NSExtensionActivationSupportsImageWithMaxCount</key>
            <integer>100</integer>
            <key>NSExtensionActivationSupportsMovieWithMaxCount</key>
            <integer>25</integer>
        </dict>
    </dict>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.share-services</string>
    <key>NSExtensionPrincipalClass</key>
    <string>CustomShareNavigationController</string>
</dict>

attempt 2: Info.plist

<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 "com.adobe.pdf"
                    || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.file-url"
                    || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url"
                    || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg"
                    || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.png"
                ).@count == $extensionItem.attachments.@count
        ).@count == 1
        </string>
    </dict>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.share-services</string>
    <key>NSExtensionPrincipalClass</key>
    <string>CustomShareNavigationController</string>
</dict>
1

There are 1 answers

1
Arthur On

When you initiate a share for a PDF in Safari, it will actually consider 2 input items: the PDF and the URL. Since your NSExtensionActivationRule predicate specifically states that @count == 1, it will return false as there is more than 1 element that matches your predicate. The fix is thus to change @count == 1 to @count >= 1 or whichever logic best fits your application.

Updated query that worked for me:

<key>NSExtensionActivationRule</key>
<string>
SUBQUERY (
    extensionItems,
    $extensionItem,
        SUBQUERY (
            $extensionItem.attachments,
            $attachment,
            ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.adobe.pdf"
            || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.file-url"
            || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url"
            || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg"
            || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.png"
        ).@count == $extensionItem.attachments.@count
).@count >= 1
</string>