3

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:

  1. Create macOS ReaderTranslatorAppKit bundle and set principalClass="ReaderTranslatorAppKit.ReaderTranslatorAppKit" in Info.plist
open class ReaderTranslatorAppKit: NSObject, ReaderTranslatorCommonInterfaces {
    public func test() -> String {
        "test 1"
    }
}
  1. Added ReaderTranslatorAppKit bundle in Embed as macOS platform Added it to plugin

  2. Create ReaderTranslatorCommonInterfaces common protocol

public protocol ReaderTranslatorCommonInterfaces {
    func test() -> String
}
  1. 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

Victor Kushnerov
  • 3,706
  • 27
  • 56

0 Answers0