我尝试使用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的文章
我的步骤:
ReaderTranslatorAppKit
捆绑包,然后在Info.plist中设置principalClass =“ ReaderTranslatorAppKit.ReaderTranslatorAppKit” open class ReaderTranslatorAppKit: NSObject, ReaderTranslatorCommonInterfaces {
public func test() -> String {
"test 1"
}
}
public protocol ReaderTranslatorCommonInterfaces {
func test() -> String
}
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,在两个项目之间共享代码并使用CFNotificationCenterGetDarwinNotifyCenter
和UserDefaults(suitename:)
将对象从扩展程序发送到应用程序here