【问题标题】:Proximity state not changing after receiving notification收到通知后接近状态没有改变
【发布时间】:2015-06-19 10:13:11
【问题描述】:

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

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

我唯一的 ViewController 中的代码是(视图是在故事板中创建的):

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. 按“开始检查”按钮启用接近监控并禁用空闲计时器。
  3. 盖住接近传感器,这会关闭屏幕。
  4. 等待大约 2 分钟(这也有影响,过早不会重现问题)
  5. 向自己发送通知(例如 iMessage)

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

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

【问题讨论】:

    标签: ios iphone swift


    【解决方案1】:

    我已经用 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()
    

    【讨论】:

      【解决方案2】:

      我向苹果工程师发送了一个技术支持问题,他们告诉我这是操作系统中的一个错误。没有关于何时修复的报告,但请注意此问题似乎出现在(至少)iOS 7 和 8 的所有版本中,并且似乎没有解决方法。如果我收到修复通知,我会在这里更新。

      【讨论】:

        【解决方案3】:

        实际上有解决方法.. UIDevice.current.proximityState 正在改变,因为它应该改变。但问题在于UIDeviceProximityStateDidChange 通知。 因此,解决方法是创建一个计时器,每 x 次检查一次 UIDevice.current.proximityState 是否发生了变化。只是不要使用UIDeviceProximityStateDidChange

        【讨论】:

        • 这不起作用。即使您使用线程在没有通知的情况下检查状态,除非您设置了proximityMonitoringEnabled(这将强制屏幕禁用),否则状态不会改变。
        猜你喜欢
        • 1970-01-01
        • 2013-11-28
        • 1970-01-01
        • 1970-01-01
        • 2021-06-18
        • 1970-01-01
        • 2014-07-17
        • 1970-01-01
        • 2021-05-24
        相关资源
        最近更新 更多