Detect screen on/off from iOS service

后端 未结 3 1979
盖世英雄少女心
盖世英雄少女心 2020-11-30 03:38

I am developing a network monitor app that runs in background as a service. Is it possible to get a notification/call when the screen is turned on or off?

It exists

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 04:13

    Using the lower-level notify API you can query the lockstate when a notification is received:

    #import 
    
    int notify_token;
    notify_register_dispatch("com.apple.springboard.lockstate", ¬ify_token, dispatch_get_main_queue(), ^(int token) {
        uint64_t state = UINT64_MAX;
        notify_get_state(token, &state);
        NSLog(@"com.apple.springboard.lockstate = %llu", state);
    });
    

    Of course your app will have to start a UIBackgroundTask in order to get the notifications, which limits the usefulness of this technique due to the limited runtime allowed by iOS.

提交回复
热议问题