Detect screen on/off from iOS service

后端 未结 3 1958
盖世英雄少女心
盖世英雄少女心 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 03:54

    While iPhone screen is locked appdelegate method "- (void)applicationWillResignActive:(UIApplication *)application" will be called you can check that. Hope it may help you.

    0 讨论(0)
  • 2020-11-30 04:13

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

    #import <notify.h>
    
    int notify_token;
    notify_register_dispatch("com.apple.springboard.lockstate", &notify_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.

    0 讨论(0)
  • 2020-11-30 04:16

    You can use Darwin notifications, to listen for the events. I'm not 100% sure, but it looks to me, from running on a jailbroken iOS 5.0.1 iPhone 4, that one of these events might be what you need:

    com.apple.iokit.hid.displayStatus
    com.apple.springboard.hasBlankedScreen
    com.apple.springboard.lockstate
    

    Update: also, the following notification is posted when the phone locks (but not when it unlocks):

    com.apple.springboard.lockcomplete
    

    To use this, register for the event like this (this registers for just one event, but if that doesn't work for you, try the others):

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
                                    NULL, // observer
                                    displayStatusChanged, // callback
                                    CFSTR("com.apple.iokit.hid.displayStatus"), // event name
                                    NULL, // object
                                    CFNotificationSuspensionBehaviorDeliverImmediately);
    

    where displayStatusChanged is your event callback:

    static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
        NSLog(@"event received!");
        // you might try inspecting the `userInfo` dictionary, to see 
        //  if it contains any useful info
        if (userInfo != nil) {
            CFShow(userInfo);
        }
    }
    

    If you really want this code to run in the background as a service, and you're jailbroken, I would recommend looking into iOS Launch Daemons. As opposed to an app that you simply let run in the background, a launch daemon can start automatically after a reboot, and you don't have to worry about iOS rules for apps running tasks in the background.

    Let us know how this works!

    0 讨论(0)
提交回复
热议问题