在后台运行时测量iPhone高度 [英] Measure iPhone altitude whilst running in background

查看:117
本文介绍了在后台运行时测量iPhone高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift编写一个iPhone应用程序,该应用程序可以跟踪飞机的起降(如气压变化所指示).

I am using Swift to write an iPhone App which tracks an aircraft taking off and landing (as indicated by a change in air pressure).

为此,我需要测量iPhone在后台运行时的气压.我需要至少每隔一小时或一分钟测量一次压力,持续时间为一小时或2分钟.我已阅读

For this I need to measure the iPhone's air pressure whilst it is running in the background. I need to measure the pressure at intervals of at least once every minute for a period of an hour or 2. I have read the Apple Developer Background Execution guide, but I don't think any of the background modes are relevant; for example, a finite-length task does not last long enough and a fetch task seems to be too infrequent.

我当前获取气压的代码如下:

My current code to obtain air pressure is as follows:

let opQueue = OperationQueue.current!
opQueue.qualityOfService = .background
altimeter.startRelativeAltitudeUpdates(to: opQueue) { (data, error) in
      print ("Pressure returned")
      //If pressure returned from iPhone correctly:
      if let data = data {
          self.localPressure = convertPressure(data.pressure.doubleValue, fromUnit: .kPa, toUnit: .hPa) //In hPa
      }
}

我已将此operationQueueQoS更改为.background,但是据我所知,这只是将其作为获得更新&的较低优先级.与作为后台执行运行无关(对吗?).

I have changed the QoS of this operationQueue to .background, but as I understand it, this just makes this a lower priority to get updated & has nothing to do with running as a background execution (correct?).

还有其他我可以尝试在后台执行期间定期获取气压的方法吗?我无法想象每分钟左右获取气压会消耗大量的电能,所以我希望这是可能的.

Is there anything else I can try to periodically obtain the air pressure under a background execution? I can't imagine that obtaining the air pressure every minute or so would use a huge amount of power, so I would hope that it would be possible.

任何可用的帮助将不胜感激!

Any help available would be much appreciated!

谢谢

推荐答案

您是正确的,OperationQueuecos属性仅更改其优先级,将其设置为background不会使您的应用在其中运行背景.

You are correct, the cos attribute of an OperationQueue only changes its priority, setting it to background won't make your app run in the background.

关于在后台获取高度数据,您应该使用CoreLocation库而不是CoreMotion并使用背景位置更新.

As for getting altitude data in the background, you should use the CoreLocation library rather than the CoreMotion and use background location updates.

您必须使您的类符合CLLocationManagerDelegate,开始位置更新,然后实现以下委托方法:

You have to make your class conform to CLLocationManagerDelegate, start the location updates and then implement the following delegate method:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let currentLocation = locations.last else { return }
    let altitude = currentLocation.altitude
    //use altitude to check whether plane is taking off or landing
}

在后台,系统很可能使用CMAltimeter来获取高度数据,但是没有API在后台获取高度数据,因此您必须使用CoreLocation.

Behind the scenes, the system is most probably using CMAltimeter to get the altitude data, but there is no API to get it in the background, hence you have to use CoreLocation.

这篇关于在后台运行时测量iPhone高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆