-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHealthKitManager.m
118 lines (86 loc) · 4.84 KB
/
HealthKitManager.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
#import "HealthKitManager.h"
#import <HealthKit/HealthKit.h>
@interface HealthKitManager ()
@property (nonatomic, retain) HKHealthStore *healthStore;
@end
@implementation HealthKitManager
+ (HealthKitManager *)sharedManager {
static dispatch_once_t pred = 0;
static HealthKitManager *instance = nil;
dispatch_once(&pred, ^{
instance = [[HealthKitManager alloc] init];
instance.healthStore = [[HKHealthStore alloc] init];
});
return instance;
}
- (void)requestAuthorization {
if ([HKHealthStore isHealthDataAvailable] == NO) {
// If our device doesn't support HealthKit -> return.
return;
}
NSArray *readTypes = @[[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryWater],
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCaffeine]];
NSArray *writeTypes = @[[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryWater],
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCaffeine]];
[self.healthStore requestAuthorizationToShareTypes:[NSSet setWithArray:writeTypes]
readTypes:[NSSet setWithArray:readTypes] completion:nil];
}
- (NSDate *)readBirthDate {
NSError *error;
NSDate *dateOfBirth = [self.healthStore dateOfBirthWithError:&error]; // Convenience method of HKHealthStore to get date of birth directly.
if (!dateOfBirth) {
NSLog(@"Either an error occured fetching the user's age information or none has been stored yet. In your app, try to handle this gracefully.");
}
return dateOfBirth;
}
- (void)writeWaterSample:(CGFloat)weight {
// Each quantity consists of a value and a unit.
HKUnit *milliLitersUnits = [HKUnit literUnitWithMetricPrefix:HKMetricPrefixMilli];
HKQuantity *volumeQuantity = [HKQuantity quantityWithUnit:milliLitersUnits doubleValue:weight];
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryWater];
NSDate *now = [NSDate date];
// For every sample, we need a sample type, quantity and a date.
HKQuantitySample *weightSample = [HKQuantitySample quantitySampleWithType:weightType quantity:volumeQuantity startDate:now endDate:now];
[self.healthStore saveObject:weightSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"Error while saving weight (%f) to Health Store: %@.", weight, error);
}
}];
}
- (void)writeWeightSample:(CGFloat)weight {
// Each quantity consists of a value and a unit.
HKUnit *kilogramUnit = [HKUnit gramUnitWithMetricPrefix:HKMetricPrefixKilo];
HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:kilogramUnit doubleValue:weight];
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
NSDate *now = [NSDate date];
// For every sample, we need a sample type, quantity and a date.
HKQuantitySample *weightSample = [HKQuantitySample quantitySampleWithType:weightType quantity:weightQuantity startDate:now endDate:now];
[self.healthStore saveObject:weightSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"Error while saving weight (%f) to Health Store: %@.", weight, error);
}
}];
}
- (void)writeWorkoutDataFromModelObject:(id)workoutModelObject {
// In a real world app, you would pass in a model object representing your workout data, and you would pull the relevant data here and pass it to the HealthKit workout method.
// For the sake of simplicity of this example, we will just set arbitrary data.
NSDate *startDate = [NSDate date];
NSDate *endDate = [startDate dateByAddingTimeInterval:60 * 60 * 2];
NSTimeInterval duration = [endDate timeIntervalSinceDate:startDate];
CGFloat distanceInMeters = 57000.;
HKQuantity *distanceQuantity = [HKQuantity quantityWithUnit:[HKUnit meterUnit] doubleValue:(double)distanceInMeters];
HKWorkout *workout = [HKWorkout workoutWithActivityType:HKWorkoutActivityTypeRunning
startDate:startDate
endDate:endDate
duration:duration
totalEnergyBurned:nil
totalDistance:distanceQuantity
metadata:nil];
[self.healthStore saveObject:workout withCompletion:^(BOOL success, NSError *error) {
NSLog(@"Saving workout to healthStore - success: %@", success ? @"YES" : @"NO");
if (error != nil) {
NSLog(@"error: %@", error);
}
}];
}
@end