I'm trying to do a query to the pedometer cache on an iPhone 6 with iOS 8.1.2, I'm using objective-c, I have imported CoreMotion framework and included it in the project the code looks like this
NSDate *startDate = [[NSDate date] dateByAddingTimeInterval:-60*60*12];
NSDate *endDate = [NSDate date];
CMPedometer *pedo = [[CMPedometer alloc]init];
[pedo queryPedometerDataFromDate:startDate toDate:endDate withHandler:^(CMPedometerData *pedometerData, NSError *error)
{
if (error)
{
NSLog(@"error: %@", error);
}
}];
This gives me the error: Error Domain=CMErrorDomain Code=103 "The operation couldn’t be completed. (CMErrorDomain error 103.)"
If I do the exact same thing in Swift like this
var dateString = "2014-12-15"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-DD"
var startDate = dateFormatter.dateFromString(dateString)
var endDate = NSDate()
pedometer.queryPedometerDataFromDate(startDate, toDate: endDate){
(data, error) -> Void in
if error != nil
{
println("There was an error requesting data from the pedometer: \(error)")
}
else
{
println(data)
}
}
I get the pedometer data and no errors.
In both cases I accept the popup telling me to accept the tracking physical activity. I have double checked that the app has read access to physical activity data under anonymity settings.
Can anyone explain what I'm doing wrong ?
You should hold your CMPedometer variables as a property of you class, not as Local variables. And then it will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With