在我的应用程序中,我添加了我的共享扩展,它工作正常,但是当我通过
UIActivityViewController
邀请应用程序时,我遇到一个问题,我显示了我的应用程序扩展。如何隐藏我自己的应用程序的应用程序扩展?
您可以通过在排除的活动类型列表中添加您的扩展活动类型来完成此操作:
let activityViewController = UIActivityViewController(activityItems: <your items>, applicationActivities: <your supported application activities>)
let extensionActivityType = UIActivityType(<your extension activity type id>)
activityViewController.excludedActivityTypes = [extensionActivityType]
首先,在代码中定义以下几行
class ActionExtensionBlockerItem: NSObject, UIActivityItemSource {
func activityViewController(_ activityViewController: UIActivityViewController, dataTypeIdentifierForActivityType activityType: UIActivityType?) -> String {
return "com.your.unique.uti";
}
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
// Returning an NSObject here is safest, because otherwise it is possible for the activity item to actually be shared!
return NSObject()
}
func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivityType?) -> String {
return ""
}
func activityViewController(_ activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: UIActivityType?, suggestedSize size: CGSize) -> UIImage? {
return nil
}
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return ""
}
}
这里 com.your.unique.uti 是您的应用程序组标识符
然后在演示时
activityViewController
使用下面的代码
let activityViewController = UIActivityViewController(activityItems: [/* Other Items To Share, */ ActionExtensionBlockerItem()], applicationActivities: nil)
我根据https://pspdfkit.com/blog/2016/hiding-action-share-extensions-in-your-own-apps/
遇到了这个问题1、定义方法调配:
// MARK: - Hide Share Extension app from host app
/**
https://pspdfkit.com/blog/2016/hiding-action-share-extensions-in-your-own-apps/
*/
final class ActionExtensionBlockerItem: NSObject, UIActivityItemSource {
public func activityViewController(_ activityViewController: UIActivityViewController, dataTypeIdentifierForActivityType activityType: UIActivity.ActivityType?) -> String {
return "YOUR_APP_GROUP_NAME"
}
public func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
// Returning an NSObject here is safest, because otherwise it is possible for the activity item to actually be shared!
return NSObject()
}
public func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivity.ActivityType?) -> String {
return ""
}
public func activityViewController(_ activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: UIActivity.ActivityType?, suggestedSize size: CGSize) -> UIImage? {
return nil
}
public func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return ""
}
}
extension UIActivityViewController {
public static func doSwizzling() {
MethodSwizzleGivenClassName(cls: self, originalSelector: #selector(UIActivityViewController.init(activityItems:applicationActivities:)), overrideSelector: #selector(UIActivityViewController.init(aw_activityItems:applicationActivities:)))
}
@objc
private convenience init(aw_activityItems: [Any], applicationActivities: [UIActivity]?) {
if aw_activityItems.last is ActionExtensionBlockerItem {
self.init(aw_activityItems: aw_activityItems, applicationActivities: applicationActivities)
} else {
let activityItems = aw_activityItems + [ActionExtensionBlockerItem()]
self.init(aw_activityItems: activityItems, applicationActivities: applicationActivities)
}
}
}
2、在应用程序启动时进行方法调配
UIActivityViewController.doSwizzling()
3、在扩展应用的info.plist的“NSExtension”键的字典中添加激活规则
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<string>
SUBQUERY (
extensionItems,
$extensionItem,
SUBQUERY (
$extensionItem.attachments,
$attachment,
(ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.image"
|| ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.audiovisual-content"
|| ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.archive"
|| ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.disk-image"
|| (SUBQUERY (
$attachment.registeredTypeIdentifiers,
$uti,
NOT $uti UTI-CONFORMS-TO "public.folder" AND NOT $uti UTI-CONFORMS-TO "public.url" AND NOT $uti UTI-CONFORMS-TO "dyn.age8u" AND NOT $uti UTI-CONFORMS-TO "dyn.age81y3psq6"
).@count > 0
AND ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.file-url")
) AND NOT (ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "YOUR_APP_GROUP_NAME")
).@count == $extensionItem.attachments.@count
).@count == 1
</string>
</dict>