iOS AppExtension:如何结合NSExtensionActivationRule和NSPredicate

时间:2014-10-06 14:38:15

标签: ios ios-app-extension

我目前正在开发一个包含Share扩展名的iOS应用程序。

我意识到了 NSExtensionActivationSupportsImageWithMaxCount密钥不允许我在Safari下的.jpeg或.png网址(" public.image" UTI,kUTTypeImage)上激活我的共享扩展程序(即:imgur链接)。

如果我切换到NSActivationRule = TRUEPREDICATE,我仍然可以激活并测试我的扩展程序,但禁止发布的应用程序。

我填写了一个关于雷达的错误,如果它不被通缉(甚至facebook,twitter等等......都没有在这个URL上激活)

现在,我想结合以下键和" public.image"在NSPredicate字符串中,如文档所述(https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW8

所以我必须将密钥翻译成UTI

到目前为止,我已翻译过:
  - NSExtensionActivationSupportsFileWithMaxCount到" public.file-url" kUTTTypeFileURL
  - NSExtensionActivationSupportsMovieWithMaxCount至" public.movi​​e" kUTTypeMovie
  - NSExtensionActivationSupportsText至" public.text" kUTTypeText
  - NSExtensionActivationSupportsWebURLWithMaxCount至" public.url" kUTTypeURL

但我找不到类型翻译:

  • NSExtensionActivationSupportsWebPageWithMaxCount," public.HTML"是kUTTypeHTML?

有人已经在谓词中使用了这个键吗?

1 个答案:

答案 0 :(得分:12)

我最终做的是1)暂时允许TRUEPREDICATE,并使用像这样的逻辑。

    NSExtensionItem *item = extensionContext.inputItems.firstObject;

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

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

这将为您提供要共享的所有文档类型(例如:“public.url”)。由于多种类型,我的谓词变得有点复杂:

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

这基本上会查找图像(adobe psd除外),pdf,txt,csv或doc / docx的任何文件类型。 它也只允许一次共享1个文档。

看起来kUTTypeImage包含PSD - 因此我阻止了该格式(“com.adobe.photoshop-image”)。