Introduced
Cmpedometer: can access user activity (can access the step data) applicable to iOS8 above the system iOS can see the following description
HealthKit: A health application in the iOS system needs to be licensed to access health data (such as steps)
What's the difference between these two?
In fact, HealthKit is also the use of Cmpedometer to read the user's steps and mileage, of course, the calculation method is in the phone's internal calculation, it is to use the iphone5s above the M processor to obtain data calculations, so 5s below the device is not supported Cmpedometer Mobile phone steps, of course, Daniel can use the gravity Sensor and acceleration sensor can write their own pedometer software, I am a rookie can only use the system of pull
HealthKit steps have a shortcoming, the steps are not allowed, some people will ask: this HealthKit and I the following demo is not all using cmpedometer to get the number of steps, why HealthKit do not allow it?
A: That's because of the time zone problem recorded by HealthKit, the time zone for iOS acquisition is 8 hours less than China's time zone, for example, if I want to record the May 5 data should be xxxx-05-05 00:00:00 to xxxx-05-05 23:59:59 This interval of the number of steps, and HealthKit records of the time zone is xxxx-05-04 16:00:00 to xxxx-05-05 15:59:59, so HealthKit recorded information is not allowed, if the development of their own step software, we will write their own, not To get HealthKit's information.
The following demo is written in Swift language
Defining a global variable will report an error if it is not a global variable
let pedonmeter: CMPedometer = CMPedometer ()
Write in the method, either method can be used, you can see the documentation if you don't understand
// Judge whether the device supports the step counting function
if CMPedometer.isStepCountingAvailable () {
//Starting time
let startTime = getStartTime ()
//End Time
let endTime = getEndTime ()
//The first
// Get data in a time range up to 7 days.Parameters start time, end time, a closure
pedonmeter.queryPedometerDataFromDate (startTime, toDate: endTime, withHandler: {(pedometerData: CMPedometerData ?, error: NSError?)-> Void in
if error! = nil {
print ("error: \ (error)")
}
else {
print ("Start time: \ (startTime)")
print ("end time: \ (endTime)")
print ("steps === \ (pedometerData! .numberOfSteps)")
print ("distance === \ (pedometerData! .distance)")
}
})
}
// The second kind
// Get the data from the specified start time to the current timeParameter Start time, a closure
pedonmeter.startPedometerUpdatesFromDate (startTime, withHandler: {(pedometerData: CMPedometerData ?, error: NSError?)-> Void in
if error! = nil {
print ("error: \ (error)")
}
else {
print ("Start time: \ (startTime)")
print ("end time: \ (endTime)")
print ("steps === \ (pedometerData! .numberOfSteps)")
print ("distance === \ (pedometerData! .distance)")
}
})
Here is the way to get time, I was to get the day, you can also change their own
/**
get time of current time zone/
func getendtime ()-> nsdate
{
//conversion cost time zone let
date = NSDate () let
zone = Nstimezone.systemtimezone () let
interval = zone.secondsfromgmtfordate (date) let
nowdate = Date.datebyaddingtimeinterval Double (interval) return
nowdate
}
/**
get start time 0:0 0 seconds
* *
func getstarttime ()-> nsdate
{Let
Datef = NSDateFormatter ()
Datef.dateformat = ' yyyy-mm-dd ' let
stringdate = Datef.stringfromdate ( Getendtime ())
print ("Day date: \ (stringdate)") let
tdate = datef.datefromstring (stringdate)
// Gets the day of the local time zone 0:0 0 seconds let
zone = Nstimezone.systemtimezone () let
interval = zone.secondsfromgmtfordate (tdate!)
Let Nowday = tdate!. Datebyaddingtimeinterval (Double (interval)) return
Nowday
}
Execution results:
I walked 3107 paces a day, 2 miles away.
Note: If you like to support iOS7, then you can't use Cmpedometer,cmpedometer's predecessor was Cmstepcounter.
Summarize
The above is the iOS implementation pedometer function of the entire content, hope this article content for everyone to learn to develop iOS can help, if there are questions welcome everyone to leave a message.