UIApplication
has the handy applicationState
property that allows you to see if your application is in the background or not. In action (and share) extensions, use of UIApplication
is illegal, and even if you do get an instance of it within an extension it always reports applicationState=UIApplicationStateBackground
. I also tried observing UIApplicationDidEnterBackgroundNotification
, it never fires.
Is there a way to effectively tell if an extension is running within an app that's in the foreground or background?
Post iOS 8.2, You can observe the notifications listed here.
NSExtensionHostDidBecomeActiveNotification
NSExtensionHostWillResignActiveNotification
NSExtensionHostDidEnterBackgroundNotification
NSExtensionHostWillEnterForegroundNotification
Be sure to check that you are on a device of the appropriate version before signing up for these notifications, or your extension will crash, as they will be nil
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With