简体   繁体   中英

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. The CMPedometer API gives access to startDate and endDate of your last session. 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. 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.

Which API does that? The CMPedometer docs only specify the following method:

- 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. Specifying a start date that is more than seven days in the past returns only the available data.

I think you are looking for CMMotionActivityManager . According to the docs, getting activities from CMMotionActivityManager is pretty straightforward:

- queryActivityStartingFromDate:toDate:toQueue:withHandler:

That query will give you an array of CMMotionActivity objects. Filter by "walking = YES" and you've got your list of walking sessions for the specified time period (only works for last 7 days).

I cannot say how accurate it is or whether or not you need to do some of your own data scrubbing/filtering.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM