2016-09-06 7 views
0

私は、例えば条件、医療メモ、アレルギー&反応、すべての種類の情報のような医療IDから情報を取得しようとしています。Healthkitを使用して医療IDの情報を取得および設定しますか?

私はスイフトと健康キットを使用しています。私が得ることができる唯一の情報は、dateOfBirth、biologicalSex、およびbloodTypeです。

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. 
} 
} 
+0

コードを投稿してください! – Skoua

+0

私は自分のコード#Skouaを投稿しましたが、私はどのように医用IDへのアクセスを得ることができないのか、このコードは私にウォーキング+書き込みで書き込ませ、dateOfBirthCharacteristic、biologicalSexCharacteristic、bloodTypeCharacteristicそれは健康データからの情報です。この説明が役立つことを願っています。 – Martin

答えて

0

ユーザーの医療IDを取得または変更するためのAPIがiOSにありません。

関連する問題