好像国内不多有关于 swift
的教程和文章,基本都是 ObjectC
的,因此发一下。ios
要学Swift
的能够去看 斯坦福大学的教程, 点这里,固然,英文要好,看完就会。我就是看这个教程学会的。
另外,推荐安装Dash
这个应用,是个查阅 API 的工具软件,学Swift
开发必备
这个教程的开发环境:swift
- XCode 11.3.1
- iOS 13+
结果如图: app
以下图:
主要有async
HKObject
HKSample
这两个是抽象类,使用的时候要使用其实体类,下图中说明HKQuery
用于设置各类各样的查询例子,HKUnit
能够表示全部健康数据的单位,如:米
,英里
,卡路里
,摄氏度
等等在 Info.plist
文件中添加两个值,后面的文字说明会显示在应用请求受权的窗口中ide
Raw Key & Values
是这样 isHealthDataAvailable() -> Bool
健康
应用获取你所要操做的数据类型的受权,用户赞成以后才能处理健康数据 requestAuthorization(toShare:, read:, completion: (Bool, Error?) -> Void)
而后须要建立查询对象,要用 HKQuery
这个抽象类下面的那几个类,在上图中有说明,这里以 HKSampleQuery
(样本查询) 为例说明,不一样查询类型的对应参数不一样工具
sampleType
所要查询的样本类型,具体哪一个类型的数据:好比,体温predicate
,这个是时间的 predicate,留空时则不筛选时间limit
sortDescriptors
(query, results, error)
query
是当前查询对象, results?
是查询到的结果, error?
是发生错误时的错误信息,最主要的操做就在此处 TemperatureTableViewController.swiftspa
// // TemperatureTableViewController.swift // BodyTemparature // // Created by Kyle on 2020/2/10. // Copyright © 2020 Cyan Maple. All rights reserved. // import UIKit import HealthKit /// 获取 Health 中的体温数据 class TemperatureTableViewController: UITableViewController { // 存储查询到的数据 private var temperatureSamples: Array<HKSample> = [] private var kit: HKHealthStore! { return HKHealthStore() } private let queryType = HKQuantityType.quantityType(forIdentifier: .bodyTemperature)! private let querySample = HKSampleType.quantityType(forIdentifier: .bodyTemperature)! override func viewDidLoad() { super.viewDidLoad() navigationItem.title = "体温记录 top 10" navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(buttonPressed)) // 若是 iOS 11+ 显示大标题 if #available(iOS 11.0, *) { self.navigationController?.navigationBar.prefersLargeTitles = true } if HKHealthStore.isHealthDataAvailable(){ // Write Authorize let queryTypeArray: Set<HKSampleType> = [queryType] // Read Authorize let querySampleArray: Set<HKObjectType> = [querySample] kit.requestAuthorization(toShare: queryTypeArray, read: querySampleArray) { (success, error) in if success{ self.getTemperatureData() } else { self.showAlert(title: "Fail", message: "Unable to access to Health App", buttonTitle: "OK") } } } else { // show alert showAlert(title: "Fail", message: "设备不支持使用健康", buttonTitle: "退出") } } @objc func buttonPressed() { print("Button Pressed") // TODO: Add temperature in modal view } func getTemperatureData(){ /* // 时间查询条件对象 let calendar = Calendar.current let todayStart = calendar.date(from: calendar.dateComponents([.year,.month,.day], from: Date())) let dayPredicate = HKQuery.predicateForSamples(withStart: todayStart, end: Date(timeInterval: 24*60*60,since: todayStart!), options: HKQueryOptions.strictStartDate) */ // 建立查询对象 let temperatureSampleQuery = HKSampleQuery(sampleType: querySample, // 要获取的类型对象 predicate: nil, // 时间参数,为空时则不限制时间 limit: 10, // 获取数量 sortDescriptors: [NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)]) // 获取到的数据排序方式 { (query, results, error) in /// 获取到结果以后 results 是返回的 [HKSample]? if let samples = results { // 挨个插入到 tableView 中 for sample in samples { DispatchQueue.main.async { self.temperatureSamples.append(sample) self.tableView.insertRows(at: [IndexPath(row: self.temperatureSamples.firstIndex(of: sample)!, section:0)], with: .right ) } } } } // 执行查询操做 kit.execute(temperatureSampleQuery) } /// 自定义方法:输入 HKSample 输出 日期和温度 func getTemperatureAndDate(sample: HKSample) -> (Date, Double) { let quantitySample = sample as! HKQuantitySample let date = sample.startDate let temperature = quantitySample.quantity.doubleValue(for: .degreeCelsius()) return (date, temperature) } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return temperatureSamples.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "TemperatureCell", for: indexPath) let (date, temperature) = getTemperatureAndDate(sample: temperatureSamples[indexPath.row]) cell.textLabel?.text = String(temperature) let dateFormatter = DateFormatter() dateFormatter.dateStyle = .medium dateFormatter.timeStyle = .short dateFormatter.locale = Locale(identifier: "zh_CN") cell.detailTextLabel?.text = dateFormatter.string(from: date) return cell } // MARK: - Tool Methods - Alert func showAlert(title: String, message: String, buttonTitle: String) { let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) let okAction = UIAlertAction(title: buttonTitle, style: .default, handler: { (action) in }) alert.addAction(okAction) DispatchQueue.main.async { self.present(alert, animated: true, completion: nil) } } }