Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tell background state of action extension

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?

like image 392
Tim Johnsen Avatar asked Aug 22 '15 19:08

Tim Johnsen


1 Answers

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.

like image 99
Ben Pious Avatar answered Nov 02 '22 09:11

Ben Pious