- (void)requesthealthauthorization
{
if (![hkhealthstore ishealthdataavailable])
{
nslog(@"设备不支持healthkit"); return;
}
_healthstore = [[hkhealthstore alloc] init];
hkobjecttype *type1 = [hkobjecttype characteristictypeforidentifier:hkcharacteristictypeidentifierbiologicalsex]; // 性别
hkobjecttype *type2 = [hkobjecttype characteristictypeforidentifier:hkcharacteristictypeidentifierbloodtype];//血型
hkobjecttype *type3 = [hkobjecttype characteristictypeforidentifier:hkcharacteristictypeidentifierdateofbirth];//出生日期
hkobjecttype *type4 = [hkobjecttype characteristictypeforidentifier:hkcharacteristictypeidentifierfitzpatrickskintype];//日光反应型皮肤类型
nsset *set = [nsset setwithobjects:type1, type2, type3, type4, nil]; // 读集合
__weak typeof (&*self) weakself = self;
[_healthstore requestauthorizationtosharetypes:nil readtypes:set completion:^(bool success, nserror * _nullable error) {
if (success)
{
[weakself readhealthpersoninfo];
} else
nslog(@"healthkit不允许读写");
}
}];
}
//查询数据
- (void)readhealthpersoninfo
if ([uidevice currentdevice].systemversion.floatvalue >=9.0)
nslog(@"\n\n");
nslog(@"数据从这天%@才能读到”, [_healthstore earliestpermittedsampledate]);
nserror *error = nil;
nsdate *b = [_healthstore dateofbirthwitherror:&error];
if (!error) nslog(@"出生日期=%@", b);
else nslog(@"出生日期error=%@", error);
error = nil;
hkbiologicalsexobject *s = [_healthstore biologicalsexwitherror:&error];
if (!error) {
nslog(@"性别=%@", @(s.biologicalsex));
} else {
nslog(@"性别error=%@",error);
hkbloodtypeobject *blood = [_healthstore bloodtypewitherror:&error];
nslog(@"血型=%@", @(blood.bloodtype));
nslog(@"血型error=%@",error);
hkfitzpatrickskintypeobject *fitz = [_healthstore fitzpatrickskintypewitherror:&error];
nslog(@"日光反应型皮肤类型=%@", @(fitz.skintype));
nslog(@"日光反应型皮肤类型error=%@",error);