2

For my current project, I need to calculate the total time while an iOS device is unlocked i.e. the user is working on his device. I am aware of the Darwin Notifications as available in CFNotificationCenter as specified in this Stackoverflow question. I have tested this code on my iPhone 5C. It is woking fine for foreground mode. But I am not able to receive those notification when the application is in background mode. Is it possible to receive those notification in background mode? Or is there any other way to calculate total unlocked time for a device from my app even when the application is in background mode?

Sauvik Dolui
  • 5,520
  • 3
  • 34
  • 44

1 Answers1

0

As explained here. Background mode can only be used for specific purposes. If your App is not using background mode for any of those purposes (which it isn't), it will not work.

In short, it could only work on a jailbroken device.

Community
  • 1
  • 1
Daniel
  • 20,420
  • 10
  • 92
  • 149
  • Hi @simpleBob, even after enabling all the background modes as listed in capabilities, I failed to receive the Screen Lock Unlock notification in my project in background mode. Here is the full project https://github.com/sauvikatinnofied/iOSScreenUsages. Will you please check it out and tell me whether it is feasible in iOS? – Sauvik Dolui Feb 17 '16 at 07:06
  • @SauvikDolui are you also using those capabilities, f.e. asking for location? As stated in the documentation, sometimes it is the libraries itself that keep you alive (this might work, but it most certainly will lead to rejection for the AppStore since you are "pretending" to stay in background for a different reason than the real one). Note that by Apples definition, your App is not allowed to stay in background. – Daniel Feb 17 '16 at 07:17
  • Yes @simpleBob, I know that. But is there any other way to implement the requirement as specified in the question? At least I want a woking solution to show a demo to my client. – Sauvik Dolui Feb 17 '16 at 08:46
  • @SauvikDolui use one of the accepted capabilities (such as asking for the location), it probably will keep your app in the background – Daniel Feb 17 '16 at 08:56
  • Actually my requirement was to receive screen lock/unlock notification while they app is in background mode or is not running at all. In background mode, we can only trace significant change (500 mt) in location. But but using location tracing will not allow me to get screen lock/unlock event details. – Sauvik Dolui Feb 22 '16 at 06:18
  • @SauvikDolui the idea was that using location service will allow your app to stay in background. You can listen for events while your app is alive in background. – Daniel Feb 22 '16 at 12:18