如何设置 NSExtensionActivationRule 谓词?
Posted
技术标签:
【中文标题】如何设置 NSExtensionActivationRule 谓词?【英文标题】:How do I set NSExtensionActivationRule predicates? 【发布时间】:2015-02-22 04:32:50 【问题描述】:即使people far smarter than I am 也能找到NSExtensionActivationRule
的语法,但即使是像我这样的傻瓜也应该能够复制/粘贴示例,不是吗?不幸的是,我什至无法让Apple's examples 工作。当您点击共享表按钮时,我的操作扩展的主机应用程序崩溃(下面的堆栈跟踪)。
您应该可以将NSExtensionActivationRule
设置为字符串,对吧?当我将其设置为TRUEPREDICATE
以进行调试时,它工作正常。但是,如果我从 Apple 获取这个简单的示例(针对 public.image
稍作修改)并将其粘贴进去,我就会崩溃:
extensionItems = (
attachments = (
registeredTypeIdentifiers = (
"public.image"
);
);
)
我也试过SUBQUERY
的东西:
SUBQUERY($extensionItem.attachments, $attachment,
ANY $attachment.registeredTypeIdentifiers
UTI-CONFORMS-TO "public.image").@count >= 1
理想情况下,我想通过使用NSExtensionActivationSupportsImageWithMaxCount
来完全避免这种情况,但正如this person 发现的那样,它似乎没有为jpgs 激活。
不管怎样,堆栈跟踪如约而至:
2015-02-21 23:28:08.644 MobileSlideShow[56997:2414043] Communications error: <OS_xpc_error: <error: 0x4c985f4> count = 1, contents =
"XPCErrorDescription" => <string: 0x4c9883c> length = 22, contents = "Connection interrupted"
>
2015-02-21 23:28:08.651 MobileSlideShow[56997:2413473] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray removeObjectsAtIndexes:]: index set cannot be nil'
*** First throw call stack:
(
0 CoreFoundation 0x04060686 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x03b35a97 objc_exception_throw + 44
2 CoreFoundation 0x03f84ffe -[NSMutableArray removeObjectsAtIndexes:] + 654
3 UIKit 0x01d71d78 _UIApplicationExtensionDiscoveryGetPostprocessedExtensions + 350
4 UIKit 0x01d718e3 -[_UIActivityApplicationExtensionDiscovery activitiesMatchingInputItems:error:updateBlock:] + 1237
5 UIKit 0x01c342f2 -[UIActivityViewController _availableActivitiesForItems:applicationExtensionActivities:] + 594
6 UIKit 0x01c3409b -[UIActivityViewController _availableActivitiesForItems:] + 48
7 UIKit 0x01c3519a -[UIActivityViewController _availableActivities] + 57
8 UIKit 0x01c39552 -[UIActivityViewController activityGroupViewController:availableActivitiesInCategory:] + 65
9 UIKit 0x0195c47f -[UIActivityGroupViewController collectionView:didSelectItemAtIndexPath:] + 251
10 UIKit 0x01b8d94d -[UICollectionView _selectItemAtIndexPath:animated:scrollPosition:notifyDelegate:] + 591
11 UIKit 0x01baced4 -[UICollectionView _userSelectItemAtIndexPath:] + 191
12 UIKit 0x01bad0c8 -[UICollectionView touchesEnded:withEvent:] + 492
13 libobjc.A.dylib 0x03b4b7cd -[NSObject performSelector:withObject:withObject:] + 84
14 UIKit 0x0164f714 forwardTouchMethod + 270
15 UIKit 0x0164f784 -[UIResponder touchesEnded:withEvent:] + 31
16 libobjc.A.dylib 0x03b4b7cd -[NSObject performSelector:withObject:withObject:] + 84
17 UIKit 0x0164f714 forwardTouchMethod + 270
18 UIKit 0x0164f784 -[UIResponder touchesEnded:withEvent:] + 31
19 UIKit 0x014e714a -[UIWindow _sendTouchesForEvent:] + 874
20 UIKit 0x014e7c24 -[UIWindow sendEvent:] + 790
21 UIKit 0x014a5d81 -[UIApplication sendEvent:] + 242
22 UIKit 0x014b613b _UIApplicationHandleEventFromQueueEvent + 21263
23 UIKit 0x01489599 _UIApplicationHandleEventQueue + 2206
24 CoreFoundation 0x03f820ff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
25 CoreFoundation 0x03f77c0d __CFRunLoopDoSources0 + 253
26 CoreFoundation 0x03f77168 __CFRunLoopRun + 952
27 CoreFoundation 0x03f76aeb CFRunLoopRunSpecific + 443
28 CoreFoundation 0x03f7691b CFRunLoopRunInMode + 123
29 GraphicsServices 0x0089e2c9 GSEventRunModal + 192
30 GraphicsServices 0x0089e106 GSEventRun + 104
31 UIKit 0x0148d366 UIApplicationMain + 1526
32 MobileSlideShow 0x00092724 MobileSlideShow + 63268
33 libdyld.dylib 0x049dcac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
【问题讨论】:
"您应该可以将NSExtensionActivationRule
设置为字符串,对吧?"不,根据文档中的多次提及,NSExtensionActivationRule
必须是字典。您粘贴的示例是针对应用扩展项的 attachments
属性,而不是针对 info.plist 中的 NSExtensionActivationRule
。
好的,谢谢。我对此感到困惑我猜:“如果您需要进行更复杂或更具体的过滤,例如区分public.url和public.image,您可以创建一个谓词语句。然后,使用表示谓词的裸字符串作为 NSExtensionActivationRule 键的值,缺少或误解“键”。
是的,看起来您可以使用谓词字符串(那些以SUBQUERY
开头的字符串)作为NSExtensionActivationRule
的值。 (我从来不需要那样做。)但是您当前使用的字符串绝对是错误的。
当您使用SUBQUERY
语法时,您是否会遇到相同的崩溃?
是的,我愿意。但与此同时,我再次尝试使用 NSExtensionActivationSupportsImageWithMaxCount ,现在它正在拾取 public.jpeg,正如我最初所期望的那样。所以...我显然要疯了。但我现在似乎没事。非常感谢。
【参考方案1】:
这就是它的外观,对我来说效果很好,试试吧:
<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.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 "com.compuserve.gif"
|| ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.microsoft.bmp"
)
).@count == 1
).@count == 1
</string>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.ui-services</string>
</dict>
【讨论】:
太棒了,谢谢,这基本上正是我所需要的。我只需要将两个@count 更改为这样,因为我的处理多个:).@count == $extensionItem.attachments.@count ).@count > 0
这对我很有用,谢谢!我只需要将<string>com.apple.ui-services</string>
更改为<string>com.apple.share-services</string>
不起作用。错误仍然发出。无法上传到 AppStore。【参考方案2】:
我使用下面的 NSExtensionActivationRule 子查询从我的操作扩展中检索公共 url、vcard、纯文本。
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<string>
SUBQUERY (
extensionItems, $extensionItem,
SUBQUERY (
$extensionItem.attachments, $attachment,
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.vcard" || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.plain-text" || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url"
).@count >= 1
).@count > 0
</string>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.ui-services</string>
</dict>
【讨论】:
以上是关于如何设置 NSExtensionActivationRule 谓词?的主要内容,如果未能解决你的问题,请参考以下文章