gpt4 book ai didi

ios - "idleTimerDisabled"在 didFinishLaunchingWithOptions 中如何工作?

转载 作者:行者123 更新时间:2023-11-29 11:27:34 24 4
gpt4 key购买 nike

我不确定下面的代码在 appdelegate 的 didFinishLaunchingWithOptions 中是如何工作的?

[[UIApplication sharedApplication] addObserver: self forKeyPath: @"idleTimerDisabled" options: NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context: nil];

“idleTimerDisabled”究竟有什么作用?

谢谢

最佳答案

idleTimerDisabled的 Apple 文档

The default value of this property is NO. When most apps have no touches as user input for a short period, the system puts the device into a "sleep” state where the screen dims. This is done for the purposes of conserving power. However, apps that don't have user input except for the accelerometer—games, for instance—can, by setting this property to YES, disable the “idle timer” to avert system sleep.

通过将 true 分配给此值,当用户未执行任何操作(触摸、按下、滚动等...)时,iOS 不会使屏幕变暗并锁定 iPhone。 ).这方面的例子可以在游戏与其他普通应用程序中找到。与其他应用程序相比,游戏让您的 iPhone 唤醒时间更长。


对于
[[UIApplication sharedApplication] addObserver: forKeyPath: options: context:]
这是一个 Objective-c 键值观察 又名 KVO
您的代码的意思是,当有人使用任意值分配或更改 UIApplication.sharedApplication.idleTimerDisabled 时, truefalse 在这种情况下,您希望接收有关 [self observeValueForKeyPath: ofObject: change: context:] 方法签名中的赋值或更改的调用。

选项 NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew 表示,您想在更改字典中接收有关更改的旧值新值的更多信息,您可以稍后在观察方法中访问这些信息。

有关 KVO 的更多信息,请查看 this post通过 NSHipster .

AppCodathis 也有很好的解释.


结论
您的代码行的意思是,您希望收到有关对 KVO 观察方法中的 UIApplication.shared.idleTimerDisabled 属性所做更改的通知,并且您希望访问 旧值新值通过 改变字典。

关于ios - "idleTimerDisabled"在 didFinishLaunchingWithOptions 中如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57079544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com