iosobjective-cios-app-extensionreplaykit

How to know that iOS is going to kill my app extension


I am encountering some issues with a ReplayKit Broadcast Extension I am using to broadcast my screen.

I believe iOS is killing it under certain circumstances (e.g. when I sometimes lock the screen); my Broadcast Extension is not being notified of this, and does not have the chance to clean up after itself, causing problems for my application.

Does iOS notify the app extension somehow that it is going to get killed, similar to how applications are notified via UIApplicationWillTerminateNotification? In my testing, that callback doesn't fire if one observes it from the app extension itself.

Alternatively, is there a way to know from the application that the app extension has been killed?


Solution

  • I could not find such a callback after some thorough reading of the documentation, and it is my belief that it doesn't exist.

    One can get notified in the host app that the Broadcast Extension is no longer running by subscribing to UIScreenCapturedDidChangeNotification via NSNotificationCenter.addObserver. If the screen is not being recorded when the callback is triggered (can be checked with [UIScreen mainScreen].isCaptured), the Broadcast Extension is no longer running.

    However, this happens after the Broadcast Extension has been killed, so it might not be appropriate for all uses. It's the best I could find, though.