简体   繁体   English

如何使用应用程序扩展中的 IPC 将信息异步发送到 iOS 上的包含应用程序?

[英]How can I use IPC from an app extension to asynchronously send information to the containing app on iOS?

I would like to use inter-process communication in my app extension to notify its containing app when the extension makes changes to the app group's UserDefaults database.我想在我的应用扩展中使用进程间通信来在扩展对应用组的UserDefaults数据库进行更改时通知其包含的应用。 Currently, there appears to be no way to be informed when the UserDefaults database is updated by a different process, so I assume I'd have to establish IPC between the extension and the containing app.目前,似乎没有办法在UserDefaults数据库被不同的进程更新时得到通知,所以我假设我必须在扩展和包含的应用程序之间建立 IPC。 According to Apple's documentation , this seems to be possible using multiple different approaches: 根据 Apple 的文档,这似乎可以使用多种不同的方法:

Apps within a group can communicate with other members in the group using IPC mechanisms including Mach IPC, POSIX semaphores and shared memory, and UNIX domain sockets.组内的应用程序可以使用 IPC 机制与组内的其他成员通信,包括 Mach IPC、POSIX 信号量和共享 memory 和 UNIX 域 ZFFE33A3F6E35505FABA01D17FD07D641。 In macOS, use app groups to enable IPC communication between two sandboxed apps, or between a sandboxed app and a non-sandboxed app.在 macOS 中,使用应用程序组在两个沙盒应用程序之间或一个沙盒应用程序和一个非沙盒应用程序之间启用 IPC 通信。

I've done some research and it seems that most of these techniques are request-driven instead of asynchronous.我做了一些研究,似乎这些技术中的大多数都是请求驱动的,而不是异步的。 Is there a good way to use these techniques such that my extension can notify my app when it has changed the app group's UserDefaults ?有没有一种使用这些技术的好方法,以便我的扩展程序可以在更改应用程序组的UserDefaults时通知我的应用程序?

Additional information:附加信息:

  • The extension I'm building is a Broadcast Upload Extension.我正在构建的扩展是一个广播上传扩展。
  • UserDefaults.didChangeNotification does not work across processes, so it doesn't work for this purpose. UserDefaults.didChangeNotification不能跨进程工作,因此不能用于此目的。
  • A solution in Swift would be ideal, but Objective-C is fine too. Swift 中的解决方案将是理想的,但 Objective-C 也很好。

I know this is a little late but someone else might find it use full.我知道这有点晚了,但其他人可能会发现它已满。

For interprocess notifications, iOS/macOS uses Darwin Notification .对于进程间通知,iOS/macOS 使用Darwin Notification Here is a nice article elaborating on how to notify two/more processes about the changes made through one of the processes on CoreData , but the same method can be used to notify about any event. 这是一篇很好的文章,详细说明了如何通知两个/多个进程关于通过CoreData上的一个进程所做的更改,但是可以使用相同的方法来通知任何事件。 ie generate a notification on a UserDefaults update.即生成有关UserDefaults更新的通知。 I have tried it and it works really well for apps that share a AppGroup not sure if it would work for apps without AppGroup tho.我已经尝试过了,它对于共享AppGroup的应用程序非常有效,不确定它是否适用于没有AppGroup的应用程序。

Update 1: Here is an article that talks about a different approach using NSFilePresenter for Interprocess notification and also adds a comparison with DarwinNotification .更新 1:这是一篇文章,讨论了使用NSFilePresenter进行进程间通知的不同方法,并添加了与DarwinNotification的比较。 This one is a good article for an overall good understanding of the AppGroup/SharedContainers and IPC这是一篇很好的文章,可以全面了解 AppGroup/SharedContainers 和 IPC

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 iOS 扩展应用程序上使用 IPC(基于 C++)? - How to use IPC (C++ based) on iOS Extension App? App Extension iOS9:如何从共享视图向我的应用程序发送字符串/ URL? - App Extension iOS9: How can I send a string / URL from the share view to my the app? 如何在iOS App包含扩展和扩展(不是主机应用程序)之间进行通信 - How to communicate between iOS App Containing Extension and Extension (not Host App) 在 iOS 模拟器上启动 Flutter 应用程序时出错:“检索线程信息时出错:(ipc/send)无效的目标端口” - Error while launching Flutter app on iOS emulator: “Error retrieving thread information: (ipc/send) invalid destination port” 我可以通过应用扩展在主 iOS 应用中执行任务吗? - Can I perform a task in Main iOS app from the app Extension? 如何允许我的iOS应用程序将信息发送到服务器? - How do I allow my iOS app to send information to a server? 如何创建/使用共享应用程序组容器作为包含应用程序及其在ios中的扩展名之间的缓存存储 - How to create/use a shared app group container as cache storage between containing app and it's extension in ios 将文件从iOS应用扩展程序保存到包含的应用 - Saving files from an iOS app extension to the containing app 如何将数据从 chrome 扩展发送到 ios 应用程序 - how to send data from chrome extension to ios app 如何检测在 iOS 8 上的包含应用程序中启用了应用程序扩展? - How to detect an app extension is enabled in containing app on iOS 8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM