【问题标题】:In IOS 11, DeviceMotion in background stopped working在 IOS 11 中,后台的 DeviceMotion 停止工作
【发布时间】:2018-02-19 06:20:43
【问题描述】:

我的应用在后台报告并记录位置、高度、旋转和加速度计数据 (DeviceMotion)。这在 ios 10.3.3 上运行良好。在 IOS 11 上,当设备被锁定时,我不再有权访问运动数据。不过,海拔数据和位置数据仍在流式传输到控制台。

IOS 11 中的某些更改阻止我访问运动数据,或者我是否正在尝试以 Apple 现在阻止的方式访问它,例如 OperationQueue.main

这是我开始动态更新的方式。如果手机解锁,一切正常。如果我锁定手机,则不再更新。:

let motionManager = self.motionManager

    if motionManager.isDeviceMotionAvailable {
        motionUpdateInterval = 0.15
        motionManager.deviceMotionUpdateInterval = motionUpdateInterval

        motionManager.startDeviceMotionUpdates(using: .xArbitraryZVertical, to: OperationQueue.main) {deviceMotion, error in
            guard let deviceMotion = deviceMotion else { return }

我找不到有关 Motion 背景模式更改的任何信息,但似乎必须有一种方法,否则 RunKeeper,Strava 会中断。有人可以在 IOS11 发布之前帮我重新开始工作吗?

谢谢!

【问题讨论】:

  • 我自己也有类似的问题。您是否使用 CoreLocation 后台模式来保持应用程序在动态更新时处于活动状态?
  • @doovers 正确。 CoreLocation 正在使用中,并且之前已支持运动所需的背景模式。令人沮丧的是,WatchKit 有一个锻炼模式,允许在后台访问运动。
  • 似乎我们并不孤单......检查这个thread。您在哪些设备上看到问题?我试过 6 和 7,只有 7 不起作用。
  • 我已经看了几天这个帖子了。我有一个从周日开始开放的 Apple 支持请求。如果有事我会回帖。请也这样做。
  • 大家好。我昨天收到一封来自 Apple 的电子邮件,说这个问题可能会在 11.1 Beta 2 中得到解决。昨晚安装了它,应用程序看起来不错。我在使用 Xcode Beta 时遇到了不同的编译问题,还无法在控制台中验证内容,但请查看 11.1 Beta 2。

标签: core-motion ios11 devicemotion


【解决方案1】:

也遇到过这个问题。

我们的解决方案是确保我们启用并运行另一种后台模式(在我们的情况下是位置更新 + 音频),并在切换背景/前景时重新启动核心运动更新。

代码示例:

import UIKit
import CoreMotion

final class MotionDetector {
    private let motionManager = CMMotionManager()
    private let opQueue: OperationQueue = {
        let o = OperationQueue()
        o.name = "core-motion-updates"
        return o
    }()

    private var shouldRestartMotionUpdates = false

    init() {
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(appDidEnterBackground),
                                               name: .UIApplicationDidEnterBackground,
                                               object: nil)
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(appDidBecomeActive),
                                               name: .UIApplicationDidBecomeActive,
                                               object: nil)
    }

    deinit {
        NotificationCenter.default.removeObserver(self,
                                                  name: .UIApplicationDidEnterBackground,
                                                  object: nil)
        NotificationCenter.default.removeObserver(self,
                                                  name: .UIApplicationDidBecomeActive,
                                                  object: nil)
    }

    func start() {
        self.shouldRestartMotionUpdates = true
        self.restartMotionUpdates()
    }

    func stop() {
        self.shouldRestartMotionUpdates = false
        self.motionManager.stopDeviceMotionUpdates()
    }

    @objc private func appDidEnterBackground() {
        self.restartMotionUpdates()
    }

    @objc private func appDidBecomeActive() {
        self.restartMotionUpdates()
    }

    private func restartMotionUpdates() {
        guard self.shouldRestartMotionUpdates else { return }

        self.motionManager.stopDeviceMotionUpdates()
        self.motionManager.startDeviceMotionUpdates(using: .xArbitraryZVertical, to: self.opQueue) { deviceMotion, error in
            guard let deviceMotion = deviceMotion else { return }
            print(deviceMotion)
        }
    }
}

【讨论】:

  • 谢谢玛尼。因为我一直在旅行,所以我没有机会尝试这个。苹果昨天发布了 11.1 Beta 2,初步效果不错。我需要做更多的测试,但要感谢您的分享。
  • 它似乎工作正常,虽然我认为不需要激活背景音频模式。
  • 我使用的是 iOS 11.4.1,当应用程序进入后台时核心 Motion 更新停止。我也启用了核心定位背景模式,但没有成功
  • 谢谢。首先 - 谢谢。我在这个问题上挣扎了几天。但是 - 这个答案在最新的 iPhone(X、Xs、8 等)和最新的操作系统版本(iOS 12.1.2 上仍然可行吗?而且 - 你跳过了通知处理程序的实现。他们做了什么?调用“restartMotionUpdates( ) ? 将“shouldRestartMotionUpdates”设置为 true?谁将它设置为 false(当我们不需要它时)。
  • 嘿@MottiShneor - 我们目前在我们的生产应用程序中使用这个版本。通知处理程序已实现shouldRestartMotionUpdates 用于决定是否应该重新启动处理程序(隐藏复杂性),因为调用类只需要调用startstop。为了节省电池/等,你应该打电话给start/stop
【解决方案2】:

官方 11.1 版本修复了这个问题,我从 iPhone 8 用户那里听说原来的实现对他们有用。

11.2 测试版没有破坏任何东西。

【讨论】:

  • 我现在有多个报告,它仍然适用于 11.1 版本。但这是使用 .xMagneticNorthZVertical 设置。我已要求 Apple 重新打开错误报告。
  • 在 iOS 12.3.1 上仍然存在同样的问题,仅适用于 .xArbitraryZVertical 设置,在旧版本中是否已解决?
猜你喜欢
  • 2016-09-30
  • 2018-05-02
  • 2018-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多