I'm using UNNotificationServiceExtension
in my Swift app for iOS and it is working OK. For special cases I need to know if my app is running when the method didReceive
from UNNotificationServiceExtension
is getting called. I tried writing to UserDefaults
a bool value that indicates the app state, which is being modified by AppDelegate
events, for example setting it to false when applicationWillTerminate
is called, but sometimes app can be killed by system without calling applicationWillTerminate
, so my variable in UserDefaults
will indicate that app is still alive. Is there any better/smarter way to reach the main app state from iOS UNNotificationServiceExtension
?
Asked
Active
Viewed 586 times
2

inthy
- 362
- 6
- 16
1 Answers
-2
if you have implemented/override didReceive
of the UNNotificationServiceExtension
in AppDelegate
then, there you can simply check like this
let state = UIApplication.shared.applicationState

Neel Bhasin
- 749
- 1
- 7
- 22
-
1No, it won't work. I get following error: 'shared' is unavailable: Use view controller based solutions where appropriate instead – inthy Feb 07 '18 at 10:53
-
where are you accessing `didReceive`? – Neel Bhasin Feb 07 '18 at 10:58
-
I think it's not possible! – Mannopson Feb 07 '18 at 11:26
-
@NeelBhasin in my class that inherits from UNNotificationServiceExtension. It's in different package by iOS architecture, so I can't reach UIApplication.shared in such an obvious way – inthy Feb 07 '18 at 12:05