NSExtension - Share - Limit type only to video or only to pictures
Asked Answered
S

2

9

I have an extension in my app that allows users to pick pictures or video of their 'Photos/Pictures' app to publish in my app.

I limit the number of pictures to 20 and the limit of videos to 1 by doing the following : enter image description here

However, I want my users to select multiple pictures or only one video, which is not possible with this configuration.

I have read this post: NSExtension Share Extension limit Photos count

They explain that I can do a custom validation rule but I have no idea how to write it. Is there any other Activation parameter or can someone help with writing the rule?

Thanks in advance!

Splendent answered 29/11, 2018 at 16:23 Comment(0)
L
1

Here's a valid answer, oddly I didn't manage to make logical conditions (AND / OR) work in a single subquery so I had to split it :

SUBQUERY (
    extensionItems,
    $extensionItem,
    SUBQUERY(
        $extensionItem.attachments,
        $attachment,
        ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.movie"
    ).@count == 1
    AND
    SUBQUERY(
        $extensionItem.attachments,
        $attachment,
        ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.image"
    ).@count == 0
).@count == 1
OR
SUBQUERY (
    extensionItems,
    $extensionItem,
    SUBQUERY(
        $extensionItem.attachments,
        $attachment,
        ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.movie"
    ).@count == 0
    AND
    SUBQUERY(
        $extensionItem.attachments,
        $attachment,
        ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.image"
    ).@count <= 20
).@count >= 1
Lanctot answered 21/3, 2019 at 16:54 Comment(2)
where are you writing the above code, i am writing in info.plist of exetnsion under NSExtensionActivationRule like so between <string> subquery here </string> but i am getting error in xcode saying unable to read property list from fileWardell
I put this code in the info.plist of my extension like so : <key>NSExtension</key> <dict> <key>NSExtensionAttributes</key> <dict> <key>NSExtensionActivationRule</key> <string> SUBQUERY </string> <All closing tags here>Lanctot
S
8

Apple offers an option to do things like you want. We can take a look at the docs here:

https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW8

I modified the UTI-CONFORMS-TO items to video and images, and the counts a bit:

SUBQUERY (
    extensionItems,
    $extensionItem,
    SUBQUERY (
        $extensionItem.attachments,
        $attachment,
        ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.image"
    ).@count &lt;= 20
).@count &gt;= 1
OR
SUBQUERY (
    extensionItems,
    $extensionItem,
    SUBQUERY(
        $extensionItem.attachments,
        $attachment,
        ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.video"
    ).@count == 1
).@count == 1

This query should limit the selection to only 1 video or 1 to 20 images. Note that you should specify this query like the following:

<key>NSExtensionAttributes</key>
<dict>
    <key>NSExtensionActivationRule</key>
    <string>
    --- SUBQUERY HERE ---
    </string>
</dict>

I did not test this code and it might not fully work, but it will point you in the right direction.

Stage answered 27/2, 2019 at 15:26 Comment(4)
Thanks for the explanation. However I want to limit it to 1 video OR 20 images. Not one or one. If I delete the last @count == 1, will it take the other restrictions in my Info.Plist or will I be able to add 5 videos for example ?Splendent
I see, that was not very clear for me. Give me some time and I will update my answer :)Stage
I juste realised that I made a mistake in the description. I edit the question. Sorry about that !Splendent
Take a look, I updated the answer. (It will point you in a direction, I could not test it)Stage
L
1

Here's a valid answer, oddly I didn't manage to make logical conditions (AND / OR) work in a single subquery so I had to split it :

SUBQUERY (
    extensionItems,
    $extensionItem,
    SUBQUERY(
        $extensionItem.attachments,
        $attachment,
        ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.movie"
    ).@count == 1
    AND
    SUBQUERY(
        $extensionItem.attachments,
        $attachment,
        ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.image"
    ).@count == 0
).@count == 1
OR
SUBQUERY (
    extensionItems,
    $extensionItem,
    SUBQUERY(
        $extensionItem.attachments,
        $attachment,
        ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.movie"
    ).@count == 0
    AND
    SUBQUERY(
        $extensionItem.attachments,
        $attachment,
        ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.image"
    ).@count &lt;= 20
).@count &gt;= 1
Lanctot answered 21/3, 2019 at 16:54 Comment(2)
where are you writing the above code, i am writing in info.plist of exetnsion under NSExtensionActivationRule like so between <string> subquery here </string> but i am getting error in xcode saying unable to read property list from fileWardell
I put this code in the info.plist of my extension like so : <key>NSExtension</key> <dict> <key>NSExtensionAttributes</key> <dict> <key>NSExtensionActivationRule</key> <string> SUBQUERY </string> <All closing tags here>Lanctot

© 2022 - 2024 — McMap. All rights reserved.