How can I check if the screen is on or off in iOS? I'd like to know if the screen is currently on, even my app is in the background. I'd like to have an event listener on this. thanks.
Asked
Active
Viewed 6,161 times
6
-
What if I detect screen lock / unlock event instead? Can i do that? (Instead of screen on off) – user6539552 Oct 21 '16 at 09:51
3 Answers
2
In Swift 3 you can do:
override func viewDidLoad() {
super.viewDidLoad()
// Observer UIApplicationDidBecomeActive,UIApplicationDidEnterBackground
NotificationCenter.default.addObserver(
self,
selector: #selector(MyViewController.applicationDidBecomeActive(notification:)),
name: NSNotification.Name.UIApplicationDidBecomeActive,
object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(MyViewController.applicationDidEnterBackground(notification:)),
name:NSNotification.Name.UIApplicationDidEnterBackground,
object: nil)
}
func applicationDidBecomeActive(notification: NSNotification) {
// here my app did become active
}
func applicationDidEnterBackground(notification: NSNotification) {
// here my app did enter background
}
You can find more details in the official guide.
Details from the actual sources:
extension NSNotification.Name {
// These notifications are sent out after the equivalent delegate message is called
@available(iOS 4.0, *)
public static let UIApplicationDidEnterBackground: NSNotification.Name
@available(iOS 4.0, *)
public static let UIApplicationWillEnterForeground: NSNotification.Name
public static let UIApplicationDidFinishLaunching: NSNotification.Name
public static let UIApplicationDidBecomeActive: NSNotification.Name
public static let UIApplicationWillResignActive: NSNotification.Name
public static let UIApplicationDidReceiveMemoryWarning: NSNotification.Name
public static let UIApplicationWillTerminate: NSNotification.Name
public static let UIApplicationSignificantTimeChange: NSNotification.Name
public static let UIApplicationWillChangeStatusBarOrientation: NSNotification.Name // userInfo contains NSNumber with new orientation
public static let UIApplicationDidChangeStatusBarOrientation: NSNotification.Name // userInfo contains NSNumber with old orientation
// userInfo dictionary key for status bar orientation
public static let UIApplicationWillChangeStatusBarFrame: NSNotification.Name // userInfo contains NSValue with new frame
public static let UIApplicationDidChangeStatusBarFrame: NSNotification.Name // userInfo contains NSValue with old frame
// userInfo dictionary key for status bar frame
@available(iOS 7.0, *)
public static let UIApplicationBackgroundRefreshStatusDidChange: NSNotification.Name
@available(iOS 4.0, *)
public static let UIApplicationProtectedDataWillBecomeUnavailable: NSNotification.Name
@available(iOS 4.0, *)
public static let UIApplicationProtectedDataDidBecomeAvailable: NSNotification.Name
// Key in options dict passed to application:[will | did]FinishLaunchingWithOptions and info for UIApplicationDidFinishLaunchingNotification
// This notification is posted after the user takes a screenshot (for example by pressing both the home and lock screen buttons)
@available(iOS 7.0, *)
public static let UIApplicationUserDidTakeScreenshot: NSNotification.Name
}

Alessandro Ornano
- 34,887
- 11
- 106
- 133
-
Thanks. Seems this can only check the state of my app. What I need to do is to check the screen status that is on or off – user6539552 Oct 21 '16 at 08:35
-
-
There is no public API to do what do you want: warning, if you use some private API your app could be judge as not valid from Apple review, you must use the official API guidelines – Alessandro Ornano Oct 21 '16 at 08:36
-
I'd plan to use this for some research, not plan to do a public launch, so grateful if you would help me on some kind of solution, even it is private API. – user6539552 Oct 21 '16 at 08:40
-
If i use private API, can the app deploy as an AdHoc one and install it to my testers' devices? – user6539552 Oct 21 '16 at 08:40
-
No, I'm sorry but also test-flight versions should be passed from app review since test-flight became a part of itunes connect. – Alessandro Ornano Oct 21 '16 at 08:43
-
Thanks for your help. It seems that I have no way to do what I want. Actually, I just want to keep track on how long the user has the screen on..... :( – user6539552 Oct 21 '16 at 09:05
-
What if I detect screen lock / unlock event instead? Can i do that? (Instead of screen on off) – user6539552 Oct 21 '16 at 09:51
-
UIApplicationProtectedDataWillBecomeUnavailable: this appears when device is locked but warning this notification is not sent unless the device has a passcode setup. There are no notification about locked/unlocked device – Alessandro Ornano Oct 21 '16 at 10:00
1
You can try this code for getting state of screen in ios.
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
NULL, // observer
displayStatusChanged, // callback
CFSTR("com.apple.iokit.hid.displayStatus"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);

aafat
- 148
- 2
- 19
-
Sorry that I am not very understand this part of code. Would you explain more? I am new to this. I am going to write this in swift. Any advise? – user6539552 Oct 21 '16 at 08:38
-
I have also tried the code that got error on "CFSTR" and CFNotificationSuspensionBehaviorDeliverImmediately – user6539552 Oct 21 '16 at 08:42
-
All you need to know is that this isn't the answer. "com.apple.iokit.hid.displayStatus" could change and everything will break. – DranoMax May 03 '18 at 22:10