In IOS 11, DeviceMotion in background stopped working

前端 未结 2 1029
有刺的猬
有刺的猬 2021-01-04 11:18

My app reports and records location, altitude, rotation and accelerometer data (DeviceMotion) while in the background. This works fine on ios 10.3.3. On IOS 11, I no longer

相关标签:
2条回答
  • 2021-01-04 11:42

    The official 11.1 release fixed the issue and I've heard from iPhone 8 users that the original implementation is working for them.

    The 11.2 beta has not broken anything.

    0 讨论(0)
  • 2021-01-04 12:05

    Also came across this problem.

    Our solution was to ensure we have another background mode enabled and running (in our case location updates + audio) and restart core motion updates when switching background/foreground.

    Code sample:

    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)
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题