UIActivityViewController 选择 AirDrop 时 NSExtension 警告

tzo*_*oza 24 swift

我在 Xcode 控制台上收到下一个警告:

[NSExtension] Extension request contains input items but the extension point does not specify a set of allowed payload classes. The extension point's NSExtensionContext subclass must implement `+_allowedItemPayloadClasses`. This must return the set of allowed NSExtensionItem payload classes. In future, this request will fail with an error. Extension: <EXConcreteExtension: 0x283a54cc0> {id = com.apple.Sharing.AirDrop} Items: (
"<NSExtensionItem: 0x280d73a40> - userInfo: {\n    ShowNoContentView = 1;\n}"
Run Code Online (Sandbox Code Playgroud)

我实现的代码基本上是UIActivityViewController接收 CSV 文档的代码。

let activityController = UIActivityViewController(activityItems: [url], applicationActivities: nil)
    
    activityController.completionWithItemsHandler = {
        (_, _, _, _) in
    }
    
    activityController.excludedActivityTypes = [
        UIActivity.ActivityType.assignToContact,
        ...,
        ...
    ]

    activityController.popoverPresentationController?.sourceRect = self.view.frame
    activityController.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem

    self.present(activityController, animated: true)
Run Code Online (Sandbox Code Playgroud)

代码工作正常,但我想修复该警告。我只有一个通知扩展,我不知道为什么会出现这个警告。

小智 1

创建符合_allowedItemPayloadClasses协议的NSExtensionContext子类,并返回扩展允许的有效负载类集。

这是一个示例实现:

class MyExtensionContext: NSExtensionContext {
    override class func _allowedItemPayloadClasses() -> Set<AnyHashable> {
        return [NSURL.self, UIImage.self, NSString.self]
    }
}

let item = NSExtensionItem()
item.attachments = [/* attachments */]
item.setContext(MyExtensionContext())
Run Code Online (Sandbox Code Playgroud)

这应该可以解决该警告并确保您的扩展程序可以与 AirDrop 一起正常工作。