抱歉,我的格式,缺乏理解,术语等。我只学了两天,所以我仍然不太明白。
我是一个平面设计师的贸易,我正在努力使一个个人使用的应用程序,呼吁一些数据,并使它看起来更吸引我的视觉。我想为一天创建一个步骤计数,并从运行状况应用程序中提取此信息。
首先,我已经到了可以调用HealthKit StepCount来显示那天所采取的步骤的程度。只要我使用一个按钮(IBAction)来获取stepCount数据,然后输出到文本字符串(UILabel),这就成功了。(例如,输出小数点为66.0的数字,但一次只能执行一步,哈!)
现在,我只想自动填充“totalSteps UILabel”来显示步数,而不必按按钮来操作它。
我尝试了很多不同的方法,但都忘了我尝试了什么,把尝试过的代码放在了哪里,所以任何帮助和/或简短的解释都会很好!
谢谢您
import UIKit
import HealthKit
class ViewController: UIViewController {
@IBOutlet weak var totalSteps: UILabel!
@IBOutlet weak var quotePhrase: UITextView!
let healthStore = HKHealthStore()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
func authoriseHealthKitAccess(_ sender: Any) {
let healthKitTypes: Set = [
// access step count
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
]
healthStore.requestAuthorization(toShare: healthKitTypes, read: healthKitTypes) { (_, _) in
print("authrised???")
}
healthStore.requestAuthorization(toShare: healthKitTypes, read: healthKitTypes) { (bool, error) in
if let e = error {
print("oops something went wrong during authorisation \(e.localizedDescription)")
} else {
print("User has completed the authorization flow")
}
}
}
}
func getTodaysSteps(completion: @escaping (Double) -> Void) {
let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount)!
let now = Date()
let startOfDay = Calendar.current.startOfDay(for: now)
let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: now, options: .strictStartDate)
let query = HKStatisticsQuery(quantityType: stepsQuantityType, quantitySamplePredicate: predicate, options: .cumulativeSum) { (_, result, error) in
var resultCount = 0.0
guard let result = result else {
print("Failed to fetch steps rate")
completion(resultCount)
return
}
if let sum = result.sumQuantity() {
resultCount = sum.doubleValue(for: HKUnit.count())
}
DispatchQueue.main.async {
completion(resultCount)
}
}
healthStore.execute(query)
}
//Button Action Here:
@IBAction func getTotalSteps(_ sender: Any) {
getTodaysSteps { (result) in
print("\(result)")
DispatchQueue.main.async {
self.totalSteps.text = "\(result)"
}
}
}
}
最佳答案
您只需要在viewDidLoad
中调用相同的代码。当用户授予访问运行状况数据的权限时,还应该调用该代码。我在下面的代码中介绍了这两种情况。
import UIKit
import HealthKit
class ViewController: UIViewController {
@IBOutlet weak var totalSteps: UILabel!
@IBOutlet weak var quotePhrase: UITextView!
let healthStore = HKHealthStore()
override func viewDidLoad() {
super.viewDidLoad()
self.authoriseHealthKitAccess()
}
func authoriseHealthKitAccess() {
let healthKitTypes: Set = [
// access step count
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
]
healthStore.requestAuthorization(toShare: healthKitTypes, read: healthKitTypes) { [weak self] (bool, error) in
if let e = error {
print("oops something went wrong during authorisation \(e.localizedDescription)")
} else {
print("User has completed the authorization flow")
self?.updateStepsCountLabel()
}
}
}
func getTodaysSteps(completion: @escaping (Double) -> Void) {
let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount)!
let now = Date()
let startOfDay = Calendar.current.startOfDay(for: now)
let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: now, options: .strictStartDate)
let query = HKStatisticsQuery(quantityType: stepsQuantityType, quantitySamplePredicate: predicate, options: .cumulativeSum) { (_, result, error) in
var resultCount = 0.0
guard let result = result else {
print("Failed to fetch steps rate")
completion(resultCount)
return
}
if let sum = result.sumQuantity() {
resultCount = sum.doubleValue(for: HKUnit.count())
}
DispatchQueue.main.async {
completion(resultCount)
}
}
healthStore.execute(query)
}
private func updateStepsCountLabel() {
getTodaysSteps { (result) in
print("\(result)")
DispatchQueue.main.async {
self.totalSteps.text = "\(result)"
}
}
}
//Button Action Here:
@IBAction func getTotalSteps(_ sender: Any) {
updateStepsCountLabel()
}
}