[英]SwiftUI / AppKit Interaction between embedded app (e.g. extension) and main app
In my SwiftUI / AppKit application I have a main app and an extension (in my case a Finder Sync Extension) and I would like to send some data from my extension to the main application (call a method in the main app for example).在我的 SwiftUI / AppKit 应用程序中,我有一个主应用程序和一个扩展程序(在我的情况下是 Finder 同步扩展程序),我想将一些数据从我的扩展程序发送到主应用程序(例如调用主应用程序中的方法)。
The extension is embedded in the main application.扩展嵌入在主应用程序中。
I have actually found a working solution to this issue but its not really optimal.我实际上已经找到了解决这个问题的有效解决方案,但它并不是最优的。 My solution works by opening a URL in the extension and accepting the data in the main app by using an URL Scheme.我的解决方案通过在扩展中打开 URL 并使用 URL 方案在主应用程序中接受数据来工作。 (This is far from optimal, though because I need to encode the object I actually want to pass a JSON, URL encode it and then decode it on the other side) (这远非最佳,但因为我需要对 object 进行编码,我实际上想通过 JSON、URL 对其进行编码,然后在另一侧对其进行解码)
To sum up, what is the best/easiest way to send data/interact between an embedded application (eg an extension) and the main application?总而言之,在嵌入式应用程序(例如扩展)和主应用程序之间发送数据/交互的最佳/最简单方法是什么?
There are a variety of ways to do this, but most likely, you'll want the main app to write data somewhere the extension can get to it, and then read it from the extension.有多种方法可以做到这一点,但最有可能的是,您希望主应用程序将数据写入扩展程序可以访问的位置,然后从扩展程序中读取数据。
You should read through the docs for FileManager.containerURL(forSecurityApplicationGroupIdentifier:)
, which has some discussion of the app group's directory in MacOS: https://developer.apple.com/documentation/foundation/filemanager/1412643-containerurl您应该阅读FileManager.containerURL(forSecurityApplicationGroupIdentifier:)
的文档,其中讨论了 MacOS 中应用程序组的目录: https://developer.apple.com/documentation/foundation/filemanager/1412643-containerurl
Once you've got a directory that both the main app and extension can read to, you can write whatever you need there and then read it at the appropriate time from the extension.一旦你有了一个主应用程序和扩展程序都可以读取的目录,你可以在那里写你需要的任何东西,然后在适当的时间从扩展程序中读取它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.