如何在Catalyst中使用NSSharingService?

时间:2019-09-29 14:35:50

标签: swift swiftui maccatalyst

我尝试使用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 here中使用AppKit的文章

我的步骤:

  1. 创建macOS ReaderTranslatorAppKit捆绑包,然后在Info.plist中设置principalClass =“ ReaderTranslatorAppKit.ReaderTranslatorAppKit”
open class ReaderTranslatorAppKit: NSObject, ReaderTranslatorCommonInterfaces {
    public func test() -> String {
        "test 1"
    }
}
  1. 在“嵌入为macOS平台”中添加了ReaderTranslatorAppKit捆绑软件 Added it to plugin

  2. 创建ReaderTranslatorCommonInterfaces通用协议

public protocol ReaderTranslatorCommonInterfaces {
    func test() -> String
}
  1. SceneDelegate.swift中加载ReaderTranslatorAppKit
...
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)
    }
}

结果 投射cls.init() as? ReaderTranslatorCommonInterfaces时我得到零。怎么了?

更新

我通过创建macOS项目而不是Catalyst,在两个项目之间共享代码并使用CFNotificationCenterGetDarwinNotifyCenterUserDefaults(suitename:)将对象从扩展程序发送到应用程序here

0 个答案:

没有答案