gpt4 book ai didi

ios - 收到通知后接近状态不变

转载 作者:行者123 更新时间:2023-11-28 09:05:28 24 4
gpt4 key购买 nike

我的 iPhone 上的距离传感器有问题。似乎当禁用空闲计时器并启用接近监视时,屏幕(有时)会在收到通知时打开。在此之后,接近状态报告不正确,屏幕不会再次关闭。

我提供了一个示例项目来说明这个问题。但是重现的步骤非常简单。我在具有多个 iOS 版本(7.0.3 和 8.3)的多部手机(4S、5、6、6+)上进行了测试。当手机未连接到电源或调试器时,它似乎最可靠地发生。

我唯一的 ViewController 中的代码是( View 是在 Storyboard中创建的):

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var checkingToggleButton: UIButton!
@IBOutlet weak var debugLabel: UILabel!
@IBOutlet weak var screenDebugLabel: UILabel!

override func viewDidLoad() {
super.viewDidLoad()

self.checkingToggleButton.setTitle("Start checking", forState: UIControlState.Normal)
self.debugLabel.text = "Not checking"

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("proximityChanged:"), name: "UIDeviceProximityStateDidChangeNotification", object: nil)
}

@IBAction func handleCheckingToggle(sender: AnyObject) {

let enabled = !UIDevice.currentDevice().proximityMonitoringEnabled
if enabled {
self.debugLabel.text = "Checking"
self.checkingToggleButton.setTitle("Stop checking", forState: UIControlState.Normal)
} else {
self.debugLabel.text = "Not checking"
self.checkingToggleButton.setTitle("Start checking", forState: UIControlState.Normal)
}
UIApplication.sharedApplication().idleTimerDisabled = enabled
UIDevice.currentDevice().proximityMonitoringEnabled = enabled
}

func proximityChanged(notification:NSNotification)
{
if UIDevice.currentDevice().proximityState{
self.screenDebugLabel.text = "Proximity true"
println("Proximity true")
} else {
self.screenDebugLabel.text = "Proximity false"
println("Proximity false")
}
}
}

重现步骤:

  1. 在设备上构建提供的示例项目,并在断开电源和调试器的设备上运行应用程序(这似乎有所不同)。
  2. 通过按“开始检查”按钮启用 proximityMonitoring 并禁用空闲计时器。
  3. 盖上接近传感器,这会关闭屏幕。
  4. 等待大约 2 分钟(这也会有所不同,太快不会重现问题)
  5. 给自己发送一条通知(例如 iMessage)

屏幕将打开并且不会再次关闭。我们还创建了一个显示当前 proximityState 的标签,该状态(错误地)报告为 false。

GitHub 上示例项目的链接: https://github.com/TimPelgrim/ProximityTest

最佳答案

我已经用 Notification 方法测试了很多东西。正如 Asi 提到的那样,它效果不佳。解决方案是观察变量。

这个问题在 iOS 11.4.1 中仍然存在(尚未在 iOS 12 beta 上测试)。

swift 4:

// Prepare you observer variable
private var observer: NSKeyValueObservation?

// ...
// Start observing proximityState
self.observer = UIDevice.current.observe(\.proximityState) { [weak self] (device, _) in
print("State changed: \("device.proximityState")")
}

// ...
// Don't forget to call the `invalidate` method on your observer when you want to stop observing
self.observer?.invalidate()

关于ios - 收到通知后接近状态不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30935559/

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