- Debemos habilitar el uso del framework desde el portal de desarrollo https://developer.apple.com
2. En nuestro proyecto en la sección "Capabilities" debemos tener activo HealtKit
3. Comprobamos si el dispositivo tiene disponible el HelathKit y configuramos los parámetros de lectura y escritura que queremos utilizar con las siguientes líneas de código
if ([HKHealthStore isHealthDataAvailable])
{
NSSet *writeDataTypes = [self dataTypesToWrite];
NSSet *readDataTypes = [self dataTypesToRead];
healthStore = [[HKHealthStore alloc] init];
[self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
if (!success)
{
NSLog(@"%@",error);
return;
}
if(error)
{
NSLog(@"%@",error.description);
}
dispatch_async(dispatch_get_main_queue(), ^{
// Aquí podremos utilizar los datos que hemos obtenido
});
}];
}
else
{
NSLog(@"Health Kit Not authorized");
}
#pragma mark - HealthKit Permissions
- (NSSet *)dataTypesToWrite
{
HKQuantityType *heightType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
HKQuantityType *weightType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
return [NSSet setWithObjects:heightType,weightType,nil];
}
- (NSSet *)dataTypesToRead
{
HKQuantityType *heightType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
HKQuantityType *weightType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
return [NSSet setWithObjects: heightType, weightType, nil];
}
A partir de aquí, ya podríamos integrar HealthKit en nuestra App. Aunque lamentablemente, si queremos publicar nuestra App con Healthkit, debemos tener una url por cada idioma que contenga nuestra app con una política de privacidad.
No hay comentarios:
Publicar un comentario