HealthKit学习笔记-写入步数与跑步距离

import UIKit
import HealthKit

class ViewController: UIViewController {

    let healthStore = HKHealthStore()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        healthStore.requestAuthorization(toShare: dataTypesToWrite(), read: dataTypesToRead()) { (success, error) in
            print(success ? "请求成功" : "请求失败")
        }
        
        let endDate = Date()
        let startDate = Date(timeInterval: -300, since: endDate)
        
        //步行距离
        let walkQuantityConsumed = HKQuantity(unit: HKUnit.mile(), doubleValue: 40)
        let walkConsumedType = HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning)
        addDataToHealthKit(quantityType: walkConsumedType!, quantity: walkQuantityConsumed, start: startDate, end: endDate)
        //步数
        let stepQuantityConsumed = HKQuantity(unit: HKUnit.count(), doubleValue: 80000)
        let stepConsumedType = HKQuantityType.quantityType(forIdentifier: .stepCount)
        addDataToHealthKit(quantityType: stepConsumedType!, quantity: stepQuantityConsumed, start: startDate, end: endDate)
        
    }

    func dataTypesToWrite() -> Set{
        let dietaryCalorieEnergyType = HKObjectType.quantityType(forIdentifier: .dietaryEnergyConsumed)
        let activeEnergyBurnType = HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)
        let heightType = HKObjectType.quantityType(forIdentifier: .height)
        let weightType = HKObjectType.quantityType(forIdentifier: .bodyMass)
        let stepType = HKObjectType.quantityType(forIdentifier: .stepCount)
        let walkType = HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)
        return [dietaryCalorieEnergyType!, activeEnergyBurnType!, heightType!, weightType!,stepType!,walkType!]
    }
    
    func dataTypesToRead() -> Set{
        let dietaryCalorieEnergyType = HKObjectType.quantityType(forIdentifier: .dietaryEnergyConsumed)
        let activeEnergyBurnType = HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)
        let heightType = HKObjectType.quantityType(forIdentifier: .height)
        let weightType = HKObjectType.quantityType(forIdentifier: .bodyMass)
        let stepType = HKObjectType.quantityType(forIdentifier: .stepCount)
        let birthdayType = HKObjectType.characteristicType(forIdentifier: .dateOfBirth)
        let biologicalSexType = HKObjectType.characteristicType(forIdentifier: .biologicalSex)
        let walkType = HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)
        return [dietaryCalorieEnergyType!, activeEnergyBurnType!, heightType!, weightType!,stepType!,birthdayType!,biologicalSexType!,walkType!]
    }
    
    func addDataToHealthKit(quantityType: HKQuantityType, quantity: HKQuantity, start startDate: Date, end endDate: Date){
        let strName = UIDevice.current.name
        let strModel = UIDevice.current.model
        let strSysVersion = UIDevice.current.systemVersion
        let localeIdentifier = NSLocale.current.identifier
        let device = HKDevice(name: strName, manufacturer: "Apple", model: strModel, hardwareVersion: strSysVersion, firmwareVersion: strModel, softwareVersion: strSysVersion, localIdentifier: localeIdentifier , udiDeviceIdentifier: localeIdentifier)
        let consumedSample = HKQuantitySample(type: quantityType, quantity: quantity , start: startDate, end: endDate, device: device, metadata: nil)
        healthStore.save(consumedSample) { (success, error) in
            print(success ? "成功" : "失败")
            if !success{
                print(error.debugDescription)
            }
        }
    }
}

你可能感兴趣的:(HealthKit学习笔记-写入步数与跑步距离)