I wonder whether the iOS sends a notification to apps that indicates the system will put the device into a "sleep” state. Because I want my app to do something when the device is in "sleep" state.
5 Answers
The answer is "NO". You can only track application state not device state.

- 3,112
- 2
- 23
- 43
Good Answer :
I managed to receive "com.apple.springboard.lockcomplete" notification using CFNotificationCenterAddObserer. However, if my app is running in background, it cannot receive the notification. To fix this problem, I added UIBackgroundModes(= audio) to play mute audio consequently. At last, the app works well.
sophia
Notification:
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
NSLog(@"Device state: %@", state);
switch (state) {
case UIApplicationStateActive:
/* ... */
break;
case UIApplicationStateInactive:
/* Device was/is locked */
break;
case UIApplicationStateBackground:
/* User pressed home button or opened another App (from an alert/email/etc) */
break;
}
}
Enum :
UIApplicationState - The running states of an application
typedef enum {
UIApplicationStateActive,
UIApplicationStateInactive,
UIApplicationStateBackground
}
UIApplicationState
Constants
UIApplicationStateActive - The application is running in the foreground and currently receiving events. Available in iOS 4.0 and later.
UIApplicationStateInactive - The application is running in the foreground but is not receiving events. This might happen as a result of an interruption or because the application is transitioning to or from the background.
UIApplicationStateBackground - The application is running in the background.
Sources : Is it possible to distinguish between locking the device and sending an app to background?
-
Maybe I was't make it clear. I mean UIApplicationState is the app's state, not the device's. Even though My app is already in background, the device is still not in sleep state. – Sai Jun 24 '14 at 08:42
-
It's the difference between StateInactive (locked) and StateBackground (just app sleeping). – StrawHara Jun 24 '14 at 08:47
-
I managed to receive "com.apple.springboard.lockcomplete" notification using CFNotificationCenterAddObserer. However, if my app is running in background, it cannot receive the notification. To fix this problem, I added UIBackgroundModes(= audio) to play mute audio consequently. At last, the app works well. Thanks a lot! – Sai Jun 26 '14 at 03:19
-
Ok, I edit the post with your answer ;) Or you can post your answers and validate it. Because your question is still unanswered :/ – StrawHara Jun 26 '14 at 07:35
when device will go to inactive state it send a notification
- (void)applicationWillResignActive:(UIApplication *)application
This method should implement in application delegate. It will invoke automaticlaly when does goes to inactive state. It also invoke when you receive a call

- 1,247
- 1
- 10
- 27
-
What you are saying is i tHink it is private API. You can refer to the following link:http://stackoverflow.com/questions/14352228/is-there-a-away-to-detect-the-event-when-ios-device-goes-to-sleep-mode-when-the – Saurav Nagpal Jun 24 '14 at 08:45
No, the only notification may fulfill your need is UIApplicationWillResignActiveNotification
, but it will also be posted when
user pull down the notification center
there comes an alarm clock ringing
an push notification showing in an alert view.

- 7,868
- 8
- 56
- 103
"com.apple.springboard.lockcomplete" is the notification that I used to fix my problem.
To receive "com.apple.springboard.lockcomplete" notification , I used CFNotificationCenterAddObserer in applicationDidFinishLaunching.(It is said to a private api~)
However, if my app is running in background, it cannot receive the notification. To fix this problem, I added UIBackgroundModes(= audio) to play mute audio consequently. At last, the app works well.
Thanks all for your help!

- 120
- 10