Get the steps in background with iOS 7

后端 未结 1 537
日久生厌
日久生厌 2021-02-06 09:12

I\'m developing an app in which I should get the number of steps I made during a physical activity. I found this code:

- (void)countSteps {
    [[UIAccelerometer         


        
相关标签:
1条回答
  • 2021-02-06 09:48

    You cannot access the accelerometer when the display is turned off but you can monitor for location changes even when the display is off regardless of the iPhone model. According to my guess Moves app uses geolocation based changes to count steps. In the CLLocationManger docs, I read this

    In iOS 6 and later, you can defer the delivery of location data when your app is in the background. It is recommended that you use this feature in situations where your app could process the data later without any problems. For example, an app that tracks the user’s location on a hiking trail could defer updates until the user hikes a certain distance and then process the points all at once. Deferring updates helps save power by allowing your app to remain asleep for longer periods of time.

    - (void)allowDeferredLocationUpdatesUntilTraveled:(CLLocationDistance)distance timeout:(NSTimeInterval)timeout
    

    If your app is in the background and the system is able to optimize its power usage, the location manager tells the GPS hardware to store new locations internally until the specified distance or timeout conditions are met. When one or both criteria are met, the location manager ends deferred locations by calling the locationManager:didFinishDeferredUpdatesWithError: method of its delegate and delivers the cached locations to the locationManager:didUpdateLocations: method. If your app is in the foreground, the location manager does not defer the deliver of events but does monitor for the specified criteria. If your app moves to the background before the criteria are met, the location manager may begin deferring the delivery of events.

    So when you get a set of cached locations you could calculate the approximate number of steps that happened. I am just giving all my wild thoughts, its up to you!

    See this also, seems interesting https://www.cocoacontrols.com/controls/somotiondetector

    0 讨论(0)
提交回复
热议问题