iOS:我可以检测设备屏幕是否打开/关闭吗?

标签 ios iphone swift screen

如何在 iOS 中检查屏幕是打开还是关闭?我想知道屏幕当前是否打开,甚至我的应用程序是否在后台运行。我想对此有一个事件监听器。谢谢。

最佳答案

Swift 3 中,您可以:

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
}

您可以在 official guide 中找到更多详细信息.


来自实际来源的详细信息:

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:我可以检测设备屏幕是否打开/关闭吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40171495/

相关文章:

iphone - 使用 iPhone 相机

ios - UIPanGestureRecognizer : Why there is no StateEnded every time

ios - Ionic/Cordova 社交共享

iphone - 分析器报告以下代码泄漏

javascript - 在 iPad 上出现两次的标签

ios - didSelectRowAt 未触发(Swift 5)

ios - 在 SwiftUI 中构建自定义的类似 TabView 的 View

ios - 文件访问读取以前写入 NSDocumentDirectory 的图像失败

swift - 嵌入在 UIHostingController 中的 NavigationView 具有额外的安全区域插图

swift - 如何在 Swift 中创建泛型协议(protocol)?