1

I understand that this task requires Private API and the app is not designed for the AppStore.

So, is it possible to know when the system(iOS) is switching to off state and when it wakes up? Maybe it sends some system notifications about that?

Pavlo Shadov
  • 372
  • 4
  • 16

2 Answers2

1

First of all, look at these questions:

Is there a away to detect the event when iOS device goes to sleep mode (when the screen gets blackened)?

Is there a away to detect the event when iOS device goes to sleep mode (when the screen gets blackened)?

Here are couple of private API's which you can be interested in:

void SBGetScreenLockStatus (mach_port_t* port, bool *lockStatus, bool *passcodeEnabled);

Check whether device is locked and passcode protected.

Here you can find a usage: How to find the purple port for the front most application in IOS 5 and above?

Also, there was bunch of system wide notifications which are fired when device is turned on/off.

  • com.apple.mobile.keybagd.lock_status
  • com.apple.springboard.lockstate
  • com.apple.iokit.hid.displayStatus
  • com.apple.springboard.hasBlankedScreen
  • com.apple.springboard.lockcomplete

You can register to them using CFNotificationCenterAddObserver();

Community
  • 1
  • 1
Victor Ronin
  • 21,310
  • 16
  • 85
  • 171
  • Thanks, but I need not a lock screen event, but event when device is turned off. – Pavlo Shadov Jul 09 '14 at 09:31
  • I believe they are tightly related. Also com.apple.iokit.hid.displayStatus and com.apple.springboard.hasBlankedScreen are directly related to screen being turned on/off – Victor Ronin Jul 09 '14 at 16:41
  • These notifications will be sent every time device is locked. So they're probably no good here. – creker Jul 11 '14 at 07:02
  • @VictorRonin: I searched many places but have no detail about *com.apple.mobile.keybagd.lock_status*. Can you explain what exactly event called back by it? Thanks! – nahung89 Jun 26 '16 at 05:07
  • 1
    @nahung89 Disclaimer: I didn't touch private API for a couple of years and it's really long time (2 major releases of iOS). As a result, my knowledge could be outdated. As I remember this was a notification that iOS will erase in memory keys for iOS keychain. This usually happens some time after a device turned off. I believe reverse (putting this keys in the memory) happens when you unlock your device (enter PIN or fingerprint). If I am not mistaken there was some info on this in "Mac OS X and iOS Internals:" book. – Victor Ronin Jun 26 '16 at 16:10
  • @VictorRonin: Thank you! I checked and it's as you said. This event calls back when protected data change status. But it doesn't return value when access by `notify_get_state `. I will find out for another way. – nahung89 Jun 27 '16 at 04:11
  • @nahung89 Got it. I am curious. What are you building? – Victor Ronin Jun 27 '16 at 14:54
  • @VictorRonin: You could check here: http://stackoverflow.com/questions/38035617/detect-passcode-lock-state-from-home-screen. Basically it's a feature that only run after device locks (with passcode activate) and stop if device unlocks. So far I found that those Darwin event calls back in async so it's quite hard to implement. – nahung89 Jun 28 '16 at 01:26
1

I assume you're talking about notifications when device is turned on/off.

I was having the same problem and notifications were my first choice. But I found much simpler and more robust solution. You see, usually notifications are sent just one time. For example, if you're building a daemon that will listen to those notifications then you need to be sure that it will be running at the moment when device turn on notification is sent. That's the problem - you can't be sure and probably will miss notification that might help you. It just doesn't look that robust to me.

So obvious solution is to look at system uptime. You can obtain it with this [NSProcessInfo processInfo].systemUptime. In my case I don't need to know right away when device is turned off. I periodically save [NSProcessInfo processInfo].systemUptime value and current date and time at some file in device file system. When device is turned on and my daemon is launched I compare value from the file with current uptime. If current uptime is smaller then device was turned off and turned on. And from the date and time in the file I know when device was turned off.

creker
  • 8,920
  • 1
  • 28
  • 45