質問編集履歴

1

getTodaysDistanceメソッドのコードを追記しました。

2018/11/27 14:27

投稿

perpouh
perpouh

スコア299

test CHANGED
File without changes
test CHANGED
@@ -41,3 +41,63 @@
41
41
 
42
42
 
43
43
  ![イメージ説明](9eca331d625f5ad040355c79d966a5c1.png)
44
+
45
+
46
+
47
+ 【追記】
48
+
49
+ ```swift
50
+
51
+ func getTodaysDistance(completion: @escaping ([HKWorkout]) -> Void) {
52
+
53
+ let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning)!
54
+
55
+ let now = Date()
56
+
57
+ let startOfDay = NSDate(timeInterval: -60*60*24*30, since: now)
58
+
59
+ let predicate = HKQuery.predicateForSamples(withStart: startOfDay as Date, end: now, options: .strictStartDate)
60
+
61
+ let sort = [NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: true)]
62
+
63
+ let query = HKSampleQuery(sampleType: stepsQuantityType, predicate: predicate, limit: 0, sortDescriptors: sort, resultsHandler:{
64
+
65
+ (query, result, error) in
66
+
67
+
68
+
69
+ if let e = error {
70
+
71
+ print("Error: (e.localizedDescription)")
72
+
73
+ return
74
+
75
+ }
76
+
77
+ DispatchQueue.main.async {
78
+
79
+ guard let r = result else {
80
+
81
+ return
82
+
83
+ }
84
+
85
+
86
+
87
+ let workouts = r as! [HKWorkout]
88
+
89
+ print(type(of:workouts))
90
+
91
+ completion(workouts)
92
+
93
+ }
94
+
95
+ })
96
+
97
+
98
+
99
+ healthStore.execute(query)
100
+
101
+ }
102
+
103
+ ```