IOS background task detection screen on or off

I am developing an application that logs screen on / off events. This is a kind of smartphone analysis application. The whole application is to write a log like this:

2015 July 25 at 03:54:12 PM - Screen on
2015 July 25 at 03:59:38 PM - Screen off
2015 July 25 at 04:20:52 PM - Screen on
2015 July 25 at 04:22:32 PM - Screen off
...
2015 July 26 at 10:20:32 AM - Screen on
2015 July 26 at 10:22:11 AM - Screen off
2015 July 26 at 11:30:38 AM - Screen on
2015 July 26 at 10:31:02 AM - Screen off
...
  • β€œScreen on”: the user presses the β€œhome” button (or the power button) and enters the password / unlock password, if any.
  • Screen off: Press the power button to turn off the screen.

I managed to find a way to do this on Android using a broadcast receiver to capture events sent by the system. But iOS seems to have a problem, since iOS only allows background services to run for a few minutes, I'm not even sure if I can detect the on / off screen event on iOS.

, :

: " iOS ( - 8.4)?"

.

+4
1

, , IOS-, . , , .

, , , , .

iOS (, ) , , , , , .

, . , , push-, " ", , .

Daemon - , , App Store. App Store - - 2.8 (, , " " ):

2.8 , ,

, iOS ; , . , , .

Swift (2.0), Darwin, , : / iPhone. , , , , . , . , , , Apple .

( - ), :

import UIKit
import CoreFoundation

class MainViewController: UIViewController, UIWebViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // CoreFoundation compatible types
        var cfstr: CFString = "com.apple.iokit.hid.displayStatus" as NSString
        var notificationCenter = CFNotificationCenterGetDarwinNotifyCenter()

        CFNotificationCenterAddObserver(notificationCenter, nil,
            { (noti: CFNotificationCenter!, aPtr: UnsafeMutablePointer<Void>, aStr: CFString!, bPtr: UnsafePointer<Void>, aDict: CFDictionary!) -> () in
                print("got notification") }, cfstr, nil, CFNotificationSuspensionBehavior.DeliverImmediately)

    }
    // [... more stuff ...]
}
+1

Source: https://habr.com/ru/post/1599710/


All Articles