2017-05-19 1 views
0

Je sais qu'il est possible d'enregistrer un écouteur d'événement lorsqu'il y a un événement d'activation/de désactivation de l'écran. Que faire si je veux vérifier si l'écran est actuellement allumé ou éteint? Toute méthode pour moi de le vérifier?Vérifiez si l'écran actuel est activé ou désactivé dans iOS

Si je notification pour vérifier, voici l'événement qui va se passer:

Lorsque je verrouille l'écran. Il déclenchera

--- reçu notification: com.apple.springboard.hasBlankedScreen --- reçu notification: com.apple.springboard.lockcomplete --- reçu notification: com.apple.springboard.lockstate --- notification reçue: com.apple.iokit.hid.displayStatus

Quand je déverrouiller l'écran, il déclenchera

--- a reçu notification: com.apple.springboard.hasBlanke dScreen --- a reçu notification: com.apple.springboard.lockstate --- a reçu notification: com.apple.iokit.hid.displayStatus

Je ne peux pas simplement détecter lockcomplete pour voir si elle est désactivée, car il déclenchera également lockstate et displaystatus lorsque j'essaierai de verrouiller l'écran.

+0

voir ceci: http://stackoverflow.com/a/14208787/3901620 – KKRocks

+0

Oui, je connais celui-ci, et celui-ci ne se déclenche que s'il y a une action on/off par l'utilisateur. Disons que mon code fonctionne continuellement, même en arrière-plan, je veux savoir s'il y a une méthode pour savoir si l'écran est actuellement allumé ou éteint, juste une condition vrai/faux – user6539552

Répondre

0

Essayez avec:

static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) 
{ 
    CFStringRef nameCFString = (CFStringRef)name; 
    NSString *lockState = (NSString*)nameCFString; 
    NSLog(@"Darwin notification NAME = %@",name); 

    if([lockState isEqualToString:@"com.apple.springboard.lockcomplete"]) 
    { 
     NSLog(@"DEVICE LOCKED"); 
    } 
    else 
    { 
     NSLog(@"LOCK STATUS CHANGED"); 
    } 
} 

-(void)registerforDeviceLockNotification 
{ 
    //Screen lock notifications 
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center 
            NULL, // observer 
            displayStatusChanged, // callback 
            CFSTR("com.apple.springboard.lockcomplete"), // event name 
            NULL, // object 
            CFNotificationSuspensionBehaviorDeliverImmediately); 

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center 
            NULL, // observer 
            displayStatusChanged, // callback 
            CFSTR("com.apple.springboard.lockstate"), // event name 
            NULL, // object 
            CFNotificationSuspensionBehaviorDeliverImmediately); 
} 
1

Voici la solution simple

Placez le morceau ci-dessous du code dans viewDidLoad

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.applicationDidBecomeActive(notification:)), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil) 

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.applicationDidEnterBackground(notification:)), 
     name:NSNotification.Name.UIApplicationDidEnterBackground, object: nil) 

Ces méthodes est appelée lorsque l'appareil est verrouillé ou débloqué

@objc func applicationDidBecomeActive(notification: NSNotification) { 
    print("Device is unlocked") 
} 

@objc func applicationDidEnterBackground(notification: NSNotification) { 
     print("Device is locked") 
}