ios - How to read the height and weight data that we enter in the health app to our custom app -
how read height , weight data values enter in health app our custom app?
1. request healthkit authorization:
- (void)checkhealthstoreauthorization { if ([hkhealthstore ishealthdataavailable]) { nsset *readdatatypes = [self datatypestoread]; if (!self.healthstore) { self.healthstore = [[hkhealthstore alloc] init]; } static nsinteger = 0; (hkobjecttype * quantitytype in readdatatypes) { if ([self.healthstore authorizationstatusfortype:quantitytype] == hkauthorizationstatusnotdetermined) { [self.healthstore requestauthorizationtosharetypes:nil readtypes:readdatatypes completion:^(bool success, nserror *error) { i++; if (!success) { if (error) { nslog(@"requestauthorizationtosharetypes error: %@", error); } return; } else { if (i == [readdatatypes count]) { static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{ [self getheight]; [self getweight]; }); } } }]; } else { if ([quantitytype isequal:[hkobjecttype quantitytypeforidentifier:hkquantitytypeidentifierheight]]) { [self getheight]; } else if ([quantitytype isequal:[hkobjecttype quantitytypeforidentifier:hkquantitytypeidentifierbodymass]]) { [self getweight]; } } } } } - (nsset *)datatypestoread { hkquantitytype *height = [hkobjecttype quantitytypeforidentifier:hkquantitytypeidentifierheight]; hkquantitytype *weight = [hkobjecttype quantitytypeforidentifier:hkquantitytypeidentifierbodymass]; return [nsset setwithobjects:height, weight, nil]; }
2. height , weight:
- (void)getheight { nsdatecomponents *interval = [[nsdatecomponents alloc] init]; interval.month = 1; hkquantitytype *quantitytype = [hkobjecttype quantitytypeforidentifier:hkquantitytypeidentifierheight]; hkstatisticscollectionquery *query = [[hkstatisticscollectionquery alloc] initwithquantitytype:quantitytype quantitysamplepredicate:nil options:hkstatisticsoptiondiscreteaverage anchordate:[nsdate date] intervalcomponents:interval]; query.initialresultshandler = ^(hkstatisticscollectionquery *query, hkstatisticscollection *results, nserror *error) { if (error) { nslog(@"an error occurred while retrieving body mass: %@", error); } [results enumeratestatisticsfromdate:[self startdate] todate:[nsdate date] withblock:^(hkstatistics *result, bool *stop) { hkquantity *quantity = result.averagequantity; const int month = 30 * 24 * 60 * 60; static bool islastmonthresult; static double lastheight; if ([[nsdate date] timeintervalsincedate:result.startdate] < month) { islastmonthresult = yes; if (!quantity && lastheight > 0.0) { nslog(@"height: %lf", lastheight); } } if (quantity) { lastheight = [quantity doublevalueforunit:[hkunit meterunit]] * 100; if (islastmonthresult) { nslog(@"height: %lf", lastheight); } } }]; }; [self.healthstore executequery:query]; } - (void)getweight { nsdatecomponents *interval = [[nsdatecomponents alloc] init]; interval.month = 1; hkquantitytype *quantitytype = [hkobjecttype quantitytypeforidentifier:hkquantitytypeidentifierbodymass]; hkstatisticscollectionquery *query = [[hkstatisticscollectionquery alloc] initwithquantitytype:quantitytype quantitysamplepredicate:nil options:hkstatisticsoptiondiscreteaverage anchordate:[nsdate date] intervalcomponents:interval]; query.initialresultshandler = ^(hkstatisticscollectionquery *query, hkstatisticscollection *results, nserror *error) { if (error) { nslog(@"an error occurred while retrieving body mass: %@", error); } [results enumeratestatisticsfromdate:[self startdate] todate:[nsdate date] withblock:^(hkstatistics *result, bool *stop) { hkquantity *quantity = result.averagequantity; const int month = 30 * 24 * 60 * 60; static bool islastmonthresult; static double lastweight; if ([[nsdate date] timeintervalsincedate:result.startdate] < month) { islastmonthresult = yes; if (!quantity && lastweight > 0.0) { nslog(@"weight: %lf", lastweight); } } if (quantity) { lastweight = [quantity doublevalueforunit:[hkunit gramunit]] / 1000; if (islastmonthresult) { nslog(@"weight: %lf", lastweight); } } }]; }; [self.healthstore executequery:query]; } - (nsdate *)startdate { nscalendar *calendar = [nscalendar currentcalendar]; nsdatecomponents *components = [calendar components:nscalendarunitday | nscalendarunitmonth | nscalendarunityear fromdate:[nsdate date]]; components.hour = 0; components.year -= 1; nsdate *startdate = [calendar datefromcomponents:components]; return startdate; }
Comments
Post a Comment