【发布时间】:2016-09-23 05:48:56
【问题描述】:
我正在构建一个供个人使用的应用程序,但我目前正纠结于如何准确地从 healthkit 中获取昨天的步骤。然后从那里,将它放入一个变量中(应该很容易,我知道)。
我有一个 HealthKitManager 类,它从视图内部调用该函数,然后将其附加到同一视图中的变量。
我已经搜索了大多数 healthKit 问题,并获得了数据,但我认为这不是准确的数据。我昨天的手机数据是 1442 步,但它返回 2665 步。最重要的是,当我尝试将数据放入变量时,它会打印为 0。
HealthKitManagerClass
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)
}}
ViewControllerClass
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
所以,基本上我的问题是:
- 昨天的整个过程我需要什么 NSDate()?
- 如何从昨天的Steps() 中获取步骤并将它们正确放入 UpdateViewController 中的变量中?
感谢您的帮助!
【问题讨论】:
-
您的手机是否连接了第三方追踪器?
-
您的实际要求是什么?例如,如果用户手动将步骤添加到 healthkit,您是否也要阅读?
-
我的手机没有第三方追踪器,只需阅读手机健康应用程序中的步骤即可。也不需要手动添加步骤
-
所以你只想得到iphone自动检测到的步骤?
-
基本上,是的!以后如果我想让它变得更好,我会尝试使用 fitbit API,但现在我需要围绕 HealthKit 啊哈!
标签: ios swift swift2 healthkit hksamplequery