daily pastebin goal
21%
SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. override func viewDidLoad() {
  2.     super.viewDidLoad()
  3.     // Do any additional setup after loading the view, typically from a nib.
  4.  
  5.  
  6.     let walkingHR = HealthKitManager.retriveHeathKitData(dataType: "walkingHeartRate")
  7.  
  8.  
  9.     print(String(format: "%.0f", walkingHR))
  10.     setUpLabels(type: "walkingHeartRate", samples: walkingHR)
  11.  
  12. }
  13.    
  14. var comp: DateComponents = Calendar.current.dateComponents([.year, .month, .day], from: Date())
  15.         comp.timeZone = TimeZone(abbreviation: "UTC")!
  16.         let truncated = Calendar.current.date(from: comp)!
  17.  
  18.         var  beats: Double? = 0.0001
  19.  
  20.         var typeHeart = HKQuantityType.quantityType(forIdentifier: .restingHeartRate)
  21.  
  22.         switch dataType{
  23.  
  24.         case "walkingHeartRate":
  25.             typeHeart = HKQuantityType.quantityType(forIdentifier: .walkingHeartRateAverage)
  26.             break
  27.         case "restingHeartRate":
  28.             typeHeart = HKQuantityType.quantityType(forIdentifier: .restingHeartRate)
  29.             break
  30.         case "heartRateVariability":
  31.             typeHeart = HKQuantityType.quantityType(forIdentifier: .heartRateVariabilitySDNN)
  32.             break
  33.  
  34.         default:
  35.  
  36.             print("Get Heart Rate Data Type not defined", "dataType  switch 1 from retriveHeathKitData")
  37.         }
  38.  
  39.  
  40.         let startDate = truncated
  41.  
  42.         let endDate = Date()
  43.         let predicate: NSPredicate? = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: HKQueryOptions.strictEndDate)
  44.  
  45.         let squery = HKStatisticsQuery(quantityType: typeHeart!, quantitySamplePredicate: predicate, options: .discreteAverage, completionHandler: {(query: HKStatisticsQuery,result: HKStatistics?, error: Error?) -> Void in
  46.             DispatchQueue.main.async(execute: {() -> Void in
  47.  
  48.  
  49.  
  50.                 var type: String = ""
  51.  
  52.                 switch dataType{
  53.  
  54.                 case "walkingHeartRate":
  55.                     let quantity: HKQuantity? = result?.averageQuantity()
  56.                     beats = quantity?.doubleValue(for: HKUnit.count().unitDivided(by: HKUnit.minute()))
  57.                     type = "walkingHR"
  58.                     break
  59.                 case "restingHeartRate":
  60.                     let quantity: HKQuantity? = result?.averageQuantity()
  61.                     beats = quantity?.doubleValue(for: HKUnit.count().unitDivided(by: HKUnit.minute()))
  62.                     type = "restingHR"
  63.                     break
  64.                 case "heartRateVariability":
  65.                     let quantity: HKQuantity? = result?.averageQuantity()
  66.                     beats = quantity?.doubleValue(for: HKUnit.secondUnit(with: .milli))
  67.                     type = "variabilityHR"
  68.                     break
  69.                 default:
  70.                     print("Get Heart Rate Data Type not defined", "dataType  switch 2 from retriveHeathKitData")
  71.                     beats = 0.00
  72.                 }
  73.  
  74.  
  75.                 print("got:  (String(format: "%.f", beats!))")
  76.                 //print(dataType)
  77.  
  78.             })
  79.         })
  80.         healthKitStore.execute(squery)
  81.  
  82.     return(beats!)
  83.  
  84.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top