-
Notifications
You must be signed in to change notification settings - Fork 297
/
RCTAppleHealthKit+TypesAndPermissions.m
277 lines (248 loc) · 14.3 KB
/
RCTAppleHealthKit+TypesAndPermissions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//
// RCTAppleHealthKit+TypesAndPermissions.m
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "RCTAppleHealthKit+TypesAndPermissions.h"
@implementation RCTAppleHealthKit (TypesAndPermissions)
#pragma mark - HealthKit Permissions
- (nullable HKObjectType *)getReadPermFromText:(nonnull NSString*)key {
UIDevice *deviceInfo = [UIDevice currentDevice];
float systemVersion = deviceInfo.systemVersion.floatValue;
// Characteristic Identifiers
if ([@"DateOfBirth" isEqualToString: key]) {
return [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth];
}else if ([@"Height" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
}else if ([@"Weight" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
}else if ([@"BiologicalSex" isEqualToString: key]) {
return [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex];
}
// Body Measurements
if ([@"BodyMass" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
}else if ([@"BodyFatPercentage" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyFatPercentage];
}else if ([@"BodyMassIndex" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex];
}else if ([@"LeanBodyMass" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass];
}
// Fitness Identifiers
if ([@"Steps" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
}else if ([@"StepCount" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
}else if ([@"DistanceWalkingRunning" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning];
}else if ([@"DistanceCycling" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling];
}else if ([@"BasalEnergyBurned" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBasalEnergyBurned];
}else if ([@"ActiveEnergyBurned" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned];
}else if ([@"FlightsClimbed" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed];
}else if ([@"NikeFuel" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierNikeFuel];
}
// if ([@"AppleExerciseTime" isEqualToString: key]) {
// return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierAppleExerciseTime];
// }
// Nutrition Identifiers
if ([@"DietaryEnergy" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed];
}
// Vital Signs Identifiers
if ([@"HeartRate" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
}else if ([@"BodyTemperature" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyTemperature];
}else if ([@"BloodPressureSystolic" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureSystolic];
}else if ([@"BloodPressureDiastolic" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureDiastolic];
}else if ([@"RespiratoryRate" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierRespiratoryRate];
}
// Results Identifiers
if ([@"BloodGlucose" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodGlucose];
}
// Sleep
if ([@"SleepAnalysis" isEqualToString: key]) {
return [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];
}
// workouts
if ([@"MindfulSession" isEqualToString: key] && systemVersion >= 10.0) {
return [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierMindfulSession];
} else if ([@"MindfulSession" isEqualToString: key]){
return [HKObjectType workoutType];
}
return nil;
}
- (nullable HKObjectType *)getWritePermFromText:(nonnull NSString*) key {
// Body Measurements
if([@"Height" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
} else if([@"Weight" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
} else if([@"BodyMass" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
} else if([@"BodyFatPercentage" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyFatPercentage];
} else if([@"BodyMassIndex" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex];
} else if([@"LeanBodyMass" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass];
}
// Fitness Identifiers
if([@"Steps" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
} else if([@"StepCount" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
} else if([@"DistanceWalkingRunning" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning];
} else if([@"DistanceCycling" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling];
} else if([@"BasalEnergyBurned" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBasalEnergyBurned];
} else if([@"ActiveEnergyBurned" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned];
} else if([@"FlightsClimbed" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed];
}
// Nutrition Identifiers
if([@"Biotin" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryBiotin];
} else if([@"Caffeine" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCaffeine];
} else if([@"Calcium" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCalcium];
} else if([@"Carbohydrates" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCarbohydrates];
} else if([@"Chloride" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryChloride];
} else if([@"Cholesterol" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCholesterol];
} else if([@"Copper" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCopper];
} else if([@"EnergyConsumed" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed];
} else if([@"FatMonounsaturated" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatMonounsaturated];
} else if([@"FatPolyunsaturated" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatPolyunsaturated];
} else if([@"FatSaturated" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatSaturated];
} else if([@"FatTotal" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatTotal];
} else if([@"Fiber" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFiber];
} else if([@"Folate" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFolate];
} else if([@"Iodine" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryIodine];
} else if([@"Iron" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryIron];
} else if([@"Magnesium" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryMagnesium];
} else if([@"Manganese" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryManganese];
} else if([@"Molybdenum" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryMolybdenum];
} else if([@"Niacin" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryNiacin];
} else if([@"PantothenicAcid" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryPantothenicAcid];
} else if([@"Phosphorus" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryPhosphorus];
} else if([@"Potassium" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryPotassium];
} else if([@"Protein" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryProtein];
} else if([@"Riboflavin" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryRiboflavin];
} else if([@"Selenium" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietarySelenium];
} else if([@"Sodium" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietarySodium];
} else if([@"Sugar" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietarySugar];
} else if([@"Thiamin" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryThiamin];
} else if([@"VitaminA" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminA];
} else if([@"VitaminB12" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminB12];
} else if([@"VitaminB6" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminB6];
} else if([@"VitaminC" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminC];
} else if([@"VitaminD" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminD];
} else if([@"VitaminE" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminE];
} else if([@"VitaminK" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminK];
} else if([@"Zinc" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryZinc];
} else if([@"Water" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryWater];
}
// Sleep
if([@"SleepAnalysis" isEqualToString:key]) {
return [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];
}
// Mindfulness
if([@"MindfulSession" isEqualToString:key]) {
return [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierMindfulSession];
}
return nil;
}
// Returns HealthKit read permissions from options array
- (NSSet *)getReadPermsFromOptions:(NSArray *)options {
NSMutableSet *readPermSet = [NSMutableSet setWithCapacity:1];
for(int i=0; i<[options count]; i++) {
NSString *optionKey = options[i];
if(optionKey != nil){
HKObjectType *val = [self getReadPermFromText:optionKey];
if(val != nil) {
[readPermSet addObject:val];
}
}
}
return readPermSet;
}
// Returns HealthKit write permissions from options array
- (NSSet *)getWritePermsFromOptions:(NSArray *)options {
NSMutableSet *writePermSet = [NSMutableSet setWithCapacity:1];
for(int i=0; i<[options count]; i++) {
NSString *optionKey = options[i];
if(optionKey != nil){
HKObjectType *val = [self getWritePermFromText:optionKey];
if(val != nil) {
[writePermSet addObject:val];
}
}
}
return writePermSet;
}
- (HKObjectType *)getWritePermFromString:(NSString *)writePerm {
return [self getWritePermFromText:writePerm];
}
- (NSString *)getAuthorizationStatusString:(HKAuthorizationStatus)status {
switch (status) {
case HKAuthorizationStatusNotDetermined:
return @"NotDetermined";
case HKAuthorizationStatusSharingDenied:
return @"SharingDenied";
case HKAuthorizationStatusSharingAuthorized:
return @"SharingAuthorized";
}
}
@end