简体   繁体   English

SwiftUI / AppKit 嵌入式应用程序(例如扩展程序)和主应用程序之间的交互

[英]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.

相关问题 在Swift中,如何将永久变量从主iOS应用传递到扩展程序(例如,今天的扩展程序,自定义键盘) - In Swift, how would I pass a permanent variable from the main iOS app to an extension (e.g. today extension, custom keyboard) 包括自己的应用程序以使用 SwiftUI 共享其他应用程序(例如 Safari)的工作表选项 - Include own App to share sheet options of other Apps (e.g. Safari) using SwiftUI 应用扩展程序和主应用之间的共享文件 - Shared file between app extension and main app Swift 3 - UISplitViewController - 在横向中隐藏/展开主视图(例如Notes App) - Swift 3 - UISplitViewController - Hide/Expand Master View in Landscape (e.g. Notes App) 一次向 Firestore 添加大量文档(例如 1000 多个文档)时,应用程序挂起 - App hangs when adding a large number of documents to Firestore (e.g. 1000+ documents) at once iOS系统消息(例如热点)位于顶部,阻止了我的应用-如何获得通知? - iOS System message (e.g. hotspot) on top blocking my app - how to get notified? 在应用程序的第一次运行(例如教程)期间,如何使 uiviewcontroller 仅可见一次? - how can I make the uiviewcontroller visible only once during first run of the app (e.g. tutorial)? 在 iOS 14 的 SwiftUI 中的主应用程序和小部件之间共享数据 - Share data between main App and Widget in SwiftUI for iOS 14 在AppKit App Swift中使用SpriteKit - Using SpriteKit in AppKit app swift 在 iOS 8 共享扩展和主应用程序之间共享数据 - Sharing data between an iOS 8 share extension and main app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM