首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iOS11中,DeviceMotion在后台停止工作

在iOS11中,DeviceMotion在后台停止工作
EN

Stack Overflow用户
提问于 2017-09-11 12:54:46
回答 2查看 4.2K关注 0票数 11

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

在iOS11中有没有什么改变阻止我访问运动数据,或者我正在尝试以苹果公司现在阻止的方式访问它,比如OperationQueue.main

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

代码语言:javascript
复制
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 }

我找不到任何关于运动背景模式改变的东西,但似乎必须有一种方法,否则RunKeeper,斯特拉瓦将崩溃。有没有人能在IOS11发布前帮我把它修好?

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2017-09-28 16:00:46

也遇到了这个问题。

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

代码示例:

代码语言:javascript
复制
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)
        }
    }
}
票数 9
EN

Stack Overflow用户

发布于 2017-11-11 08:57:10

官方的11.1版本解决了这个问题,我从iPhone 8用户那里听说,最初的实现对他们来说是有效的。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46148399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档