-1

我正在寻找一种在应用程序处于后台或另一个应用程序在前台运行时每 10 分钟左右获取一次气压数据(而不是高度)的方法。这可能吗?如果是这样,怎么做?

如果没有,那么像Pressur这样的应用程序如何工作?

4

2 回答 2

0

Core Motion 为您提供 CMA 高度计。CMAltmeter 为您提供 CMAltitudeData。CMAltitudeData 为您提供pressure.

(在后台运行是另一回事;你不能仅仅为了使用Core Motion而在后台运行,所以你必须找到其他一些在后台运行的理由。)

于 2018-09-29T03:50:52.013 回答
0

为了接收后台 CoreMotion更新,您需要做几件事,但归结为CoreLocation在后台运行。CoreMotion 不会自行在后台运行。

  • Info.plist通过隐私字符串请求授权( “位置始终和使用时”“使用时位置”
  • 通过应用程序的项目窗格中的第二个选项卡将后台模式功能添加到您的项目中。启用位置更新。
  • 创建您需要的管理器和实例:
      let locationManager = CLLocationManager()
      let motionManager = CMMotionManager() // Not required in this case
      let altimeter = CMAltimeter()
    
  • 将您的位置管理器设置allowsBackgroundLocationUpdates为 true
  • 注册适当的回调,在这种情况下,您需要一个CMAltimeter实例,例如:
if CMAltimeter.isRelativeAltitudeAvailable() {
    altimeter.startRelativeAltitudeUpdates(to: OperationQueue.main) { (data, error) in
    /* Handle Altitude changes here */
    }
}

与其他传感器(例如motionManager.gyroUpdateInterval)不同,您似乎无法控制调用频率;您可以酌情平均或丢弃值。

我在此处的存储库中有一个(越来越少)背景传感器数据收集的简单演示。

于 2021-01-05T12:47:47.653 回答