3

我尝试使用 NSSharingService 添加我的催化剂应用程序以在 macOS 上共享操作表,但出现错误NSSharingService is unavailable in Mac Catalyst

#if targetEnvironment(macCatalyst)
extension NSSharingService { //Error: 'NSSharingService' is unavailable in Mac Catalyst
    class func shareContent ( content: [AnyObject], button: NSButton ) {
        let sharingServicePicker = NSSharingServicePicker (items: content )

        sharingServicePicker.showRelativeToRect(button.bounds, ofView: button, preferredEdge: NSRectEdge.MaxY)
    }
}
#endif

我在这里找到有关如何在 Catalyst 中使用 AppKit 的文章

我的步骤:

  1. 创建 macOSReaderTranslatorAppKit包并在 Info.plist 中设置 principalClass="ReaderTranslatorAppKit.ReaderTranslatorAppKit"
open class ReaderTranslatorAppKit: NSObject, ReaderTranslatorCommonInterfaces {
    public func test() -> String {
        "test 1"
    }
}
  1. ReaderTranslatorAppKit在作为 macOS 平台的嵌入中 添加了捆绑包将其添加到插件中

  2. 创建ReaderTranslatorCommonInterfaces通用协议

public protocol ReaderTranslatorCommonInterfaces {
    func test() -> String
}
  1. 加载 ReaderTranslatorAppKitSceneDelegate.swift
...
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    if let bundlePath = Bundle.main.builtInPlugInsURL?.appendingPathComponent("ReaderTranslatorAppKit.bundle"),
        let bundle = Bundle(url: bundlePath) {

        bundle.load()

        if let cls = bundle.principalClass as? NSObject.Type,
            let plugin = cls.init() as? ReaderTranslatorCommonInterfaces { //Error: plugin is nil
            print(plugin.test())
        }
        print(bundle)
    }
}

结果 我在铸造时得到 nil cls.init() as? ReaderTranslatorCommonInterfaces。怎么了?

更新

我通过创建 macOS 项目而不是 Catalyst、在两个项目之间共享代码以及使用并将对象从我的扩展程序发送到应用程序来解决了CFNotificationCenterGetDarwinNotifyCenterUserDefaults(suitename:)问题

4

0 回答 0