我的应用程序在后台报告和记录位置、高度、旋转和加速计数据(DeviceMotion)。这在ios 10.3.3上运行良好。在IOS 11上,当设备被锁定时,我不再能够访问运动数据。不过,高度数据和位置数据仍在传输到控制台。
在iOS11中有没有什么改变阻止我访问运动数据,或者我正在尝试以苹果公司现在阻止的方式访问它,比如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 }我找不到任何关于运动背景模式改变的东西,但似乎必须有一种方法,否则RunKeeper,斯特拉瓦将崩溃。有没有人能在IOS11发布前帮我把它修好?
谢谢!
发布于 2017-09-28 16:00:46
也遇到了这个问题。
我们的解决方案是确保启用并运行另一个后台模式(在我们的情况下,位置更新+音频),并在切换背景/前景时重新启动核心动作更新。
代码示例:
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)
}
}
}发布于 2017-11-11 08:57:10
官方的11.1版本解决了这个问题,我从iPhone 8用户那里听说,最初的实现对他们来说是有效的。
11.2测试版没有破坏任何东西。
https://stackoverflow.com/questions/46148399
复制相似问题