【问题标题】:Check if current screen is on or off in iOS检查当前屏幕在 iOS 中是打开还是关闭
【发布时间】:2017-05-19 09:39:55
【问题描述】:

我知道当有屏幕开/关事件时可以注册一个事件监听器。如果我想检查当前屏幕是打开还是关闭怎么办?有什么方法可以检查吗?

如果我使用通知来检查,这是将要发生的事件:

当我锁定屏幕时。它会触发

--- 收到通知:com.apple.springboard.hasBlankedScreen --- 收到通知:com.apple.springboard.lockcomplete --- 收到通知:com.apple.springboard.lockstate --- 收到通知:com.apple.iokit.hid.displayStatus

当我解锁屏幕时,它会触发

--- 收到通知:com.apple.springboard.hasBlankedScreen --- 收到通知:com.apple.springboard.lockstate --- 收到通知:com.apple.iokit.hid.displayStatus

我不能简单地检测 lockcomplete 以查看它当前是否关闭,因为当我尝试锁定屏幕时它也会触发 lockstate 和 displaystatus。

【问题讨论】:

  • 是的,我知道这个,而且这个只有在用户有开/关操作时才会触发。假设我的代码连续运行,即使在后台,我想知道是否有任何方法可以知道当前屏幕是打开还是关闭,只是一个真/假条件

标签: ios swift screen listener


【解决方案1】:

这是一个简单的解决方案:

将下面的代码放在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)

这些方法在设备被锁定或解锁时被调用。

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

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

【讨论】:

  • 这对大多数人来说可能无关紧要,但为了让读者更清楚,这是检查应用程序是否在前台(例如,屏幕可以解锁并且应用最小化,这种方法会认为屏幕被锁定)
【解决方案2】:

尝试:

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);
}

【讨论】:

  • 你有这个答案吗 swift 或任何链接
  • 您可以尝试使用 objc2swift 在线转换器。
【解决方案3】:
private func displayStatusChanged(center: CFNotificationCenterRef?, observer: UnsafeMutableRawPointer?, name: CFString?, object: UnsafeRawPointer?, userInfo: CFDictionaryRef?) {
let nameCFString = name
let lockState = nameCFString as String
if let aName = name {
    print("Darwin notification NAME = \(aName)")
}
if (lockState == "com.apple.springboard.lockcomplete") {
    print("DEVICE LOCKED")
} else {
    print("LOCK STATUS CHANGED")
}
}  
func registerforDeviceLockNotification() {
  CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),  /*center */nil,  /* observer */displayStatusChanged,  /* callback */"com.apple.springboard.lockcomplete",  /* event name */nil,  /* object */CFNotificationSuspensionBehavior.deliverImmediately)
  CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),  /*center */nil,  /* observer */displayStatusChanged,  /* callback */"com.apple.springboard.lockstate",  /* event name */nil,  /* object */CFNotificationSuspensionBehavior.deliverImmediately)
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 2011-09-01
    • 2011-10-20
    • 2013-03-11
    • 1970-01-01
    相关资源
    最近更新 更多