[英]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:附加信息:
UserDefaults.didChangeNotification
does not work across processes, so it doesn't work for this purpose. UserDefaults.didChangeNotification
不能跨进程工作,因此不能用于此目的。 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.