问题
I try to use NSSharingService to add my catalyst app to share actions sheet on macOS but get error 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
I find the article about how to use AppKit in Catalyst here
My steps:
- Create macOS
ReaderTranslatorAppKit
bundle and set principalClass="ReaderTranslatorAppKit.ReaderTranslatorAppKit" in Info.plist
open class ReaderTranslatorAppKit: NSObject, ReaderTranslatorCommonInterfaces {
public func test() -> String {
"test 1"
}
}
Added
ReaderTranslatorAppKit
bundle in Embed as macOS platformCreate
ReaderTranslatorCommonInterfaces
common protocol
public protocol ReaderTranslatorCommonInterfaces {
func test() -> String
}
- Load ReaderTranslatorAppKit in
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)
}
}
Result
I get nil when casting cls.init() as? ReaderTranslatorCommonInterfaces
. What's wrong?
Update
I solved my issue by creating macOS project instead of Catalyst, sharing code between two projects and using CFNotificationCenterGetDarwinNotifyCenter
and UserDefaults(suitename:)
to send objects from my extension to the app here
来源:https://stackoverflow.com/questions/58156217/how-to-use-nssharingservice-in-catalyst