我正在开发一个个人使用的应用程序,目前我正纠结于如何准确地从healthkit中获取昨天的步数,然后将其放入一个变量中(我知道,这应该很容易)。
我有一个HealthKitManager类,它从一个视图内部调用该函数,然后将其附加到同一视图中的一个变量。
我已经搜索了healthKit的大部分问题,我得到了数据,但我不认为这是准确的数据。我昨天的手机数据是1442步,但它返回2665步。最重要的是,当我试图把数据是一个变量,它打印输出为0。
运行状况工具包管理器类
import Foundation
import HealthKit
class HealthKitManager {
let storage = HKHealthStore()
init()
{
checkAuthorization()
}
func checkAuthorization() -> Bool
{
// Default to assuming that we're authorized
var isEnabled = true
// Do we have access to HealthKit on this device?
if HKHealthStore.isHealthDataAvailable()
{
// We have to request each data type explicitly
let steps = NSSet(object: HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!)
// Now we can request authorization for step count data
storage.requestAuthorizationToShareTypes(nil, readTypes: steps as? Set<HKObjectType>) { (success, error) -> Void in
isEnabled = success
}
}
else
{
isEnabled = false
}
return isEnabled
}
func yesterdaySteps(completion: (Double, NSError?) -> ())
{
// The type of data we are requesting (this is redundant and could probably be an enumeration
let type = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)
// Our search predicate which will fetch data from now until a day ago
// (Note, 1.day comes from an extension
// You'll want to change that to your own NSDate
let calendar = NSCalendar.currentCalendar()
let yesterday = calendar.dateByAddingUnit(.Day, value: -1, toDate: NSDate(), options: [])
//this is probably why my data is wrong
let predicate = HKQuery.predicateForSamplesWithStartDate(yesterday, endDate: NSDate(), options: .None)
// The actual HealthKit Query which will fetch all of the steps and sub them up for us.
let query = HKSampleQuery(sampleType: type!, predicate: predicate, limit: 0, sortDescriptors: nil) { query, results, error in
var steps: Double = 0
if results?.count > 0
{
for result in results as! [HKQuantitySample]
{
steps += result.quantity.doubleValueForUnit(HKUnit.countUnit())
}
}
//I'm unsure if this is correct as well
completion(steps, error)
print("\(steps) STEPS FROM HEALTH KIT")
//this adds the steps to my character (is this in the right place)
Player.User.Gold.addSteps(Int(steps))
}
//not 100% on what this does, but I know it is necessary
storage.executeQuery(query)
}}
视图控制器类
import UIKit
import Foundation
class UpdateViewController: UIViewController {
@IBOutlet var back: UIButton!
let HKM = HealthKitManager()
var stepsFromPhone = Double()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
back.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
HKM.yesterdaySteps(){ steps, error in
self.stepsFromPhone = steps
}
Player.User.Gold.addSteps(Int(stepsFromPhone))
print(Player.User.Gold.getSteps(), "IN PLAYER")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
的输出
print(Player.User.Gold.getSteps(), "IN PLAYER")
是
0 IN PLAYER
的输出
print("\(steps) STEPS FROM HEALTH KIT")
是
2665.0 STEPS FROM HEALTH KIT
所以,基本上我的问题是:
1.我昨天需要什么NSDate()?
1.我如何从yesterdaySteps()中获取步骤并将它们正确地放置到UpdateViewController中的变量中?
谢谢你的帮助!
2条答案
按热度按时间e4yzc0pl1#
这是我在healthStore类中使用的方法
这是我如何使用它
这是上面手动添加的步骤所使用的函数,我们将其截断以获得准确的步骤
希望能帮上忙。如果你遇到什么问题就告诉我。
r7xajy2e2#
您可以使用HKStatisticsQuery