By Martin

2016-09-06 21:35:32 8 Comments

I am trying to get the information from Medical ID for example Conditions, Medical notes, Allergies & reactions, all that kind of info.

I'm using Swift and Health Kit. For the moment the only info that I can get is dateOfBirth, biologicalSex, and bloodType.

import UIKit
import HealthKit

class ViewController: UIViewController {

let healthKitStore: HKHealthStore = HKHealthStore()

override func 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(
    let biologicalSexCharacteristic = HKCharacteristicType.characteristicTypeForIdentifier(
    let bloodTypeCharacteristic = HKCharacteristicType.characteristicTypeForIdentifier(

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

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

    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
                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 (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 ) {
        } else {
            print("The distance has been recorded! Better go check!")

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


@Allan 2016-09-07 17:46:20

There is no API on iOS for retrieving or modifying the user's Medical ID.

Related Questions

Sponsored Content

32 Answered Questions

[SOLVED] Loaded nib but the 'view' outlet was not set

4 Answered Questions

[SOLVED] Expand and Collapse tableview cells

41 Answered Questions

[SOLVED] Get the length of a String

  • 2014-06-04 12:40:01
  • Scott Walter
  • 462787 View
  • 773 Score
  • 41 Answer
  • Tags:   swift string

3 Answered Questions

1 Answered Questions

[SOLVED] TableView not displaying text with JSON data from API call

3 Answered Questions

I keep getting a use unresolved identifier error swift

1 Answered Questions

[SOLVED] Can HealthKit accept medication Data?

  • 2015-01-06 15:48:09
  • bsquared
  • 655 View
  • 0 Score
  • 1 Answer
  • Tags:   ios healthkit

1 Answered Questions

[SOLVED] Write notes on Medical ID with HealthKit

Sponsored Content