简体   繁体   中英

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. 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. According to Apple's documentation , this seems to be possible using multiple different approaches:

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. In macOS, use app groups to enable IPC communication between two sandboxed apps, or between a sandboxed app and a non-sandboxed app.

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 ?

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.
  • A solution in Swift would be ideal, but Objective-C is fine too.

I know this is a little late but someone else might find it use full.

For interprocess notifications, iOS/macOS uses 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. ie generate a notification on a UserDefaults update. 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.

Update 1: Here is an article that talks about a different approach using NSFilePresenter for Interprocess notification and also adds a comparison with DarwinNotification . This one is a good article for an overall good understanding of the AppGroup/SharedContainers and IPC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM