ios - 使用 Healthkit 获取和设置医疗 ID 中的信息?

标签 ios swift healthkit

我正在尝试从医疗 ID 中获取信息,例如病情、医疗记录、过敏和 react 等所有此类信息。

我正在使用 Swift 和 Health Kit。目前,我唯一可以获得的信息是出生日期、生物性别和血型。

import UIKit
import HealthKit

class ViewController: UIViewController {

let healthKitStore: HKHealthStore = HKHealthStore()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let healthStore: HKHealthStore? = {
        if HKHealthStore.isHealthDataAvailable() {
            return HKHealthStore()
        } else {
            return nil
        }
    }()


    //MARK: Data to write
    let bodyMass = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex)
    let run = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)

    let dataTypesToWrite = NSSet(objects:bodyMass!, run!)

    //MARK: Data to read
    let dateOfBirthCharacteristic = HKCharacteristicType.characteristicTypeForIdentifier(
        HKCharacteristicTypeIdentifierDateOfBirth)
    let biologicalSexCharacteristic = HKCharacteristicType.characteristicTypeForIdentifier(
        HKCharacteristicTypeIdentifierBiologicalSex)
    let bloodTypeCharacteristic = HKCharacteristicType.characteristicTypeForIdentifier(
        HKCharacteristicTypeIdentifierBloodType)

    let dataTypesToRead = NSSet(objects:dateOfBirthCharacteristic!,biologicalSexCharacteristic!,bloodTypeCharacteristic!)


    healthStore?.requestAuthorizationToShareTypes(dataTypesToWrite as? Set<HKSampleType>, readTypes: dataTypesToRead as? Set<HKObjectType>) { (success, error) -> Void in
        if success {
            print("success")
        }else{
            print(error?.description)
        }
    }

    var dateOfBirth: String?{
        if let dateOfBirth = try? healthStore?.dateOfBirth(){
            let dateFormater = NSDateFormatter()
            dateFormater.dateStyle = .ShortStyle
            return dateFormater.stringFromDate(dateOfBirth!)
        }
        return nil
    }

    var biologicalSex: String? {
        if let biologicalSex =  try? healthStore?.biologicalSex(){
            switch biologicalSex!.biologicalSex {
            case .Female:
                return "Female"
            case .Male:
                return "Male"
            case .NotSet:
                return nil
            default:
                return nil
            }
        }
        return nil
    }


    var bloodType: String? {
        if let bloodType = try? healthStore?.bloodType(){
            switch bloodType!.bloodType {
            case .APositive:
                return "A+"
            case .ANegative:
                return "A-"
            case .BPositive:
                return "B+"
            case .BNegative:
                return "B-"
            case .ABPositive:
                return "AB+"
            case .ABNegative:
                return "AB-"
            case .OPositive:
                return "O+"
            case .ONegative:
                return "O-"
            case .NotSet:
                return nil
            }
        }
        return nil
    }

    print(dateOfBirth)
    print(biologicalSex)
    print (bloodType)
    saveDistance(20, date: NSDate())
}

func saveDistance(distanceRecorded: Double, date: NSDate ) {

    // Set the quantity type to the running/walking distance.
    let distanceType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)

    // Set the unit of measurement to miles.
    let distanceQuantity = HKQuantity(unit: HKUnit.mileUnit(), doubleValue: distanceRecorded)

    // Set the official Quantity Sample.
    let distance = HKQuantitySample(type: distanceType!, quantity: distanceQuantity, startDate: date, endDate: date)

    // Save the distance quantity sample to the HealthKit Store.
    healthKitStore.saveObject(distance, withCompletion: { (success, error) -> Void in
        if( error != nil ) {
            print(error)
        } else {
            print("The distance has been recorded! Better go check!")
        }
    })
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

最佳答案

iOS 上没有用于检索或修改用户医疗 ID 的 API。

关于ios - 使用 Healthkit 获取和设置医疗 ID 中的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39358190/

相关文章:

ios - UISegmentController 与 UITableviewController

ios - 为什么 Swift 2.2 不支持固定大小的数组,也许 swift 3.0 支持?

ios - 在 iOS Swift 中获取 Facebook 封面照片 URL

ios - 使用 PromiseKit 的单元测试功能

swift - 健康工具包 WatchOs 3 : How do I record a mindfulSession?

ios - UICollectionView 的不可见单元格保持选中状态

ios - 在 Collection View 中显示两个不同的单元格 - Swift 2.0 iOS

ios - 隐藏另一个类的按钮

ios - 如何在 iOS 的 HealthKit 中保存 HKQuantityTypeIdentifierBodyMass 类型的样本

ios - 离开 iPhone 时在 Apple Watch 上记录和保存锻炼?