[英]iOS CoreMotion API — Is there a way to keep track of how many times someone goes walking over a period of time?
I'm trying to write a simple app that will allow me to keep track of my average walking time over a set number of days using CoreMotion API. 我正在尝试编写一个简单的应用程序,使我可以使用CoreMotion API在设定的天数内跟踪平均步行时间。 The CMPedometer API gives access to startDate and endDate of your last session.
CMPedometer API允许您访问上一个会话的startDate和endDate。 I was thinking that an easy way to do this would be to keep a 'tally' of sessions in user defaults (ie everytime the pedometer tracks your walking the tally increases by 1) and also keeping a sum of endDate - startDate time differences.
我当时想,执行此操作的一种简单方法是在用户默认设置中保持会话的“计数”(即,每次计步器跟踪您的步行计数时,计数都会增加1),并且还要保持endDate与startDate时差之和。 Then, I would just divide the total time Differences by the tally to get an average walking time.
然后,我将总时间差除以计数即可得出平均步行时间。
I'm not sure how I'd do this. 我不确定该怎么做。 Is there a way to collect this data every single time that the pedometer tracks walking?
有没有一种方法可以在计步器每次跟踪步行时收集此数据?
CMPedometer API gives access to startDate and endDate of your last session.
CMPedometer API使您可以访问上一个会话的startDate和endDate。
Which API does that? 那是哪个API? The CMPedometer docs only specify the following method:
CMPedometer文档仅指定以下方法:
- queryPedometerDataFromDate:toDate:withHandler:
Use this method to retrieve historical pedestrian data between the specified dates.
使用此方法可检索指定日期之间的历史行人数据。 This method runs asynchronously and delivers the data to the block you provide.
该方法异步运行,并将数据传递到您提供的块。 Only the past seven days worth of data is stored and available for you to retrieve.
仅存储了过去7天的数据,可供您检索。 Specifying a start date that is more than seven days in the past returns only the available data.
指定开始日期超过7天只会返回可用数据。
I think you are looking for CMMotionActivityManager . 我认为您正在寻找CMMotionActivityManager 。 According to the docs, getting activities from CMMotionActivityManager is pretty straightforward:
根据文档,从CMMotionActivityManager获取活动非常简单:
- queryActivityStartingFromDate:toDate:toQueue:withHandler:
That query will give you an array of CMMotionActivity objects. 该查询将为您提供CMMotionActivity对象数组。 Filter by "walking = YES" and you've got your list of walking sessions for the specified time period (only works for last 7 days).
按“步行=是”过滤,您将获得指定时间段内的步行会话列表(仅适用于过去7天)。
I cannot say how accurate it is or whether or not you need to do some of your own data scrubbing/filtering. 我不能说它的准确性如何,或者您是否需要做一些自己的数据清理/过滤工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.