iOS:是否可以检测设备屏幕的开/关状态? [英] iOS: Can I detect if the device screen is on/off?

查看:107
本文介绍了iOS:是否可以检测设备屏幕的开/关状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查iOS中的屏幕是打开还是关闭?我想知道屏幕当前是否在打开,即使我的应用程序在后台.我想要一个事件监听器.谢谢.

How can I check if the screen is on or off in iOS? I'd like to know if the screen is currently on, even my app is in the background. I'd like to have an event listener on this. thanks.

推荐答案

Swift 3 中,您可以执行以下操作:

In Swift 3 you can do:

override func viewDidLoad() {
        super.viewDidLoad()
        // Observer UIApplicationDidBecomeActive,UIApplicationDidEnterBackground
        NotificationCenter.default.addObserver(
                self,
                selector: #selector(MyViewController.applicationDidBecomeActive(notification:)),
                name: NSNotification.Name.UIApplicationDidBecomeActive,
                object: nil)

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

func applicationDidBecomeActive(notification: NSNotification) {
  // here my app did become active
}
func applicationDidEnterBackground(notification: NSNotification) {
  // here my app did enter background
}

您可以在实际来源的详细信息:

extension NSNotification.Name {
    // These notifications are sent out after the equivalent delegate message is called
    @available(iOS 4.0, *)
    public static let UIApplicationDidEnterBackground: NSNotification.Name
    @available(iOS 4.0, *)
    public static let UIApplicationWillEnterForeground: NSNotification.Name
    public static let UIApplicationDidFinishLaunching: NSNotification.Name
    public static let UIApplicationDidBecomeActive: NSNotification.Name
    public static let UIApplicationWillResignActive: NSNotification.Name
    public static let UIApplicationDidReceiveMemoryWarning: NSNotification.Name
    public static let UIApplicationWillTerminate: NSNotification.Name
    public static let UIApplicationSignificantTimeChange: NSNotification.Name
    public static let UIApplicationWillChangeStatusBarOrientation: NSNotification.Name // userInfo contains NSNumber with new orientation
    public static let UIApplicationDidChangeStatusBarOrientation: NSNotification.Name // userInfo contains NSNumber with old orientation
    // userInfo dictionary key for status bar orientation
    public static let UIApplicationWillChangeStatusBarFrame: NSNotification.Name // userInfo contains NSValue with new frame
    public static let UIApplicationDidChangeStatusBarFrame: NSNotification.Name // userInfo contains NSValue with old frame
    // userInfo dictionary key for status bar frame
    @available(iOS 7.0, *)
    public static let UIApplicationBackgroundRefreshStatusDidChange: NSNotification.Name
    @available(iOS 4.0, *)
    public static let UIApplicationProtectedDataWillBecomeUnavailable: NSNotification.Name
    @available(iOS 4.0, *)
    public static let UIApplicationProtectedDataDidBecomeAvailable: NSNotification.Name   
    // Key in options dict passed to application:[will | did]FinishLaunchingWithOptions and info for UIApplicationDidFinishLaunchingNotification    
    // This notification is posted after the user takes a screenshot (for example by pressing both the home and lock screen buttons)
    @available(iOS 7.0, *)
    public static let UIApplicationUserDidTakeScreenshot: NSNotification.Name
}

这篇关于iOS:是否可以检测设备屏幕的开/关状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆