检查iOS中是否打开或关闭当前屏幕

时间:2017-05-19 09:39:56

标签: ios swift screen listener

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

如果我使用通知进行检查,则会发生以下事件:

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

  

---收到通知: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。

3 个答案:

答案 0 :(得分:1)

尝试:

$(document).ready(function() {
 $('#elfinder').elfinder({
url: 'path of connector file'
// add more options here
commandsOptions: {
  edit: {
    mimes: ['text/plain', 'text/html', 'text/javascript','text/css','application/x-httpd-php'],
    editors: [{
      mimes: ['text/plain', 'text/html', 'text/javascript','text/css','application/x-httpd-php'],
      load: function(textarea) {
        var mimeType = this.file.mime;
        return CodeMirror.fromTextArea(textarea, {
          mode: mimeType,
          lineNumbers: true,
          indentUnit: 4,
          theme: "dracula",
         styleActiveLine: true,
            matchBrackets: true,
        });
      },
      save: function(textarea, editor) {
        $(textarea).val(editor.getValue());
      }
    }]
  }
  }
});
});

答案 1 :(得分: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 :(得分:0)

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