gpt4 book ai didi

ios - 从 CMPedometer 检索数据时出现问题 : "Error on message reply (Connection invalid)"

转载 作者:行者123 更新时间:2023-11-29 05:40:22 31 4
gpt4 key购买 nike

我尝试使用 CoreMotion 的 CMPedometer 获取步数,但我在日志中收到以下错误,而不是步数:消息回复错误(连接无效)。下面的示例在设备(而不是模拟器)上运行。

代码(ViewController.swift中的viewDidLoad内部):

let pedometer = CMPedometer()
if CMPedometer.isStepCountingAvailable() {
print("Step counting is available...")
let calendar = Calendar.current
let fromDate = calendar.startOfDay(for: Date())
let toDate = Date()
print("From date = \(fromDate)")
print("To date = \(toDate)")
pedometer.queryPedometerData(from: fromDate, to: toDate) { (data, error) in
print("Handler (data):")
print(data!)
}

日志:

Step counting is available...
From date = 2019-06-15 22:00:00 +0000
To date = 2019-06-15 22:48:35 +0000
2019-06-16 00:48:40.827268+0200 StepTest2[3857:1087379] [Generic_deprecated] Error on message reply (Connection invalid)

最佳答案

似乎 let pedometer = CMPedometer() 必须是非本地的,例如在 viewDidLoad 外部的类中声明。几乎所有代码:

@IBOutlet weak var stepLabel: UILabel!
let pedometer = CMPedometer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

if CMPedometer.isStepCountingAvailable() {
print("Step counting is available...")
let calendar = Calendar.current
var toDate = Date()
var fromDate = calendar.startOfDay(for: toDate)
fromDate = calendar.date(byAdding: .hour, value: -24, to: fromDate)!
toDate = calendar.date(byAdding: .hour, value: 2, to: toDate)!
print("From date = \(fromDate)")
print("To date = \(toDate)")

pedometer.queryPedometerData(from: fromDate, to: toDate) { (data, error) in
print("Handler (data):")
print(data!)
guard let activityData = data, error == nil else {
print("There was an error getting the data: \(error!)")
return
}
DispatchQueue.main.async {
self.stepLabel.text = "Steps and distance: \(activityData.numberOfSteps) \(activityData.distance ?? -1)"
}
}
}
}

关于ios - 从 CMPedometer 检索数据时出现问题 : "Error on message reply (Connection invalid)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56614791/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com