我尝试使用 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 的文章
我的步骤:
- 创建 macOS
ReaderTranslatorAppKit
包并在 Info.plist 中设置 principalClass="ReaderTranslatorAppKit.ReaderTranslatorAppKit"
open class ReaderTranslatorAppKit: NSObject, ReaderTranslatorCommonInterfaces {
public func test() -> String {
"test 1"
}
}
public protocol ReaderTranslatorCommonInterfaces {
func test() -> String
}
- 加载 ReaderTranslatorAppKit
SceneDelegate.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、在两个项目之间共享代码以及使用并将对象从我的扩展程序发送到应用程序来解决了CFNotificationCenterGetDarwinNotifyCenter
我UserDefaults(suitename:)
的问题