我正在尝试创建一个应用程序,显示M7芯片在iPhone 5s上使用核心动作检测到的过去7天的步数。然而,我遇到的问题是,我试图获取数据的任何一天都会得到0步。
目前我只想将步数计入array
,主要是出于测试原因。我的viewDidLoad
方法中包含以下内容。
...
var mySteps = [Int]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
for day in 1..<30 {
mySteps.append((stepReporter.getStepsFrom(getDate(daysFromToday: day))))
println("\(mySteps[day])")
}
}
上面使用的getDate
方法只允许我通过这样做让一天回来:
func getDate(daysFromToday numberOfDays : Int) -> NSDate{
var now = NSDate()
var calendar = NSCalendar.autoupdatingCurrentCalendar()
var components = calendar.components(NSCalendarUnit.DayCalendarUnit
| NSCalendarUnit.MonthCalendarUnit
| NSCalendarUnit.YearCalendarUnit,
fromDate: now)
var beginningOfToday = calendar.dateFromComponents(components)
//println("the date marked is \(beginningOfToday)")
components.day = -1 * numberOfDays
components.month = 0
components.year = 0
var newDate : NSDate = calendar.dateByAddingComponents(components, toDate: beginningOfToday, options: nil)
return newDate
}
stepReporter
是使用CMStepCounter
对象获取步数的类的实例
func getStepsFrom(date : NSDate) -> Int{
var dayAfter = getDateDayAfter(date)
stepCounter.queryStepCountStartingFrom(date, to: dayAfter, toQueue: stepQueue, withHandler: {numberOfSteps, error in
if error{
println(" \(error.localizedDescription)")
}
else{
//self.numberOfStepsThatDay = numberOfSteps
println("the number of steps from \(date) to \(newDate) is = \(numberOfSteps)")
}
})
return numberOfStepsThatDay
}
控制台正在打印以下内容
the number of steps from 2014-07-22 04:00:00 +0000 to 2014-07-23 04:00:00 +0000 is = 270
the number of steps from 2014-07-08 04:00:00 +0000 to 2014-07-09 04:00:00 +0000 is = 0
the number of steps from 2014-07-07 04:00:00 +0000 to 2014-07-08 04:00:00 +0000 is = 0
the number of steps from 2014-07-06 04:00:00 +0000 to 2014-07-07 04:00:00 +0000 is = 0
the number of steps from 2014-07-21 04:00:00 +0000 to 2014-07-22 04:00:00 +0000 is = 0
the number of steps from 2014-07-05 04:00:00 +0000 to 2014-07-06 04:00:00 +0000 is = 0
因此,我获得了最近一天的正确数据,但其他所有日期都获得了0。我错过了什么或者我做错了什么? stepQueue
中没有其他任何内容正在运行。我也使用了CMPedometer
并获得了相同的结果。任何建议将不胜感激。谢谢!
答案 0 :(得分:0)
我安装了3个使用M7芯片的不同应用程序,并且可以确认步数与我从应用程序获得的数量相同。考虑到我之前安装的上一步计数器应用程序给了我更多我认为更准确的数据,我感到很担心。这可能是iOS 8测试版的问题,或者恢复手机后可能没有历史数据。谢谢