I am trying to create a Health app and I started using my actual iPhone as a simulator for the app. I have got it connected to the project but there is one problem. I have a feature that collects and displays health features from the user’s local ‘Health’ app. It worked on the iOS simulator when I first started testing my app but it doesn’t seem to collect it when I use the app on my actual iPhone. Moreover, it asks for permissions on the simulator but it doesn’t seem to do so on my iPhone.
Here is the relevant code:
var types = [
HealthDataType.ACTIVE_ENERGY_BURNED,
HealthDataType.STEPS,
];
var permissions = [
HealthDataAccess.READ,
HealthDataAccess.READ,
];
void getCaloriesBurned() async {
var now = DateTime.now();
var midnight = DateTime(now.year, now.month, now.day);
var strings = [];
List<HealthDataPoint> healthData =
await health.getHealthDataFromTypes(midnight, now, [types[0]]);
await health.requestAuthorization(types, permissions: permissions);
if (healthData.isEmpty) {
showBanner('No data available. Log some calories into the Health app!');
return;
}
for (var dataPoint in healthData) {
strings.add(dataPoint.value.toString());
}
for (var string in strings) {
caloriesBurned += double.parse(string).round();
}
await addCaloriesBurnedToFirestore(caloriesBurned);
setState(() {
caloriesBurned = caloriesBurned;
});
}
getSteps() async {
var now = DateTime.now();
var midnight = DateTime(now.year, now.month, now.day);
int? healthData = await health.getTotalStepsInInterval(midnight, now);
await health.requestAuthorization(types, permissions: permissions);
if (healthData != null) {
setState(() {
totalSteps = healthData.toDouble().round();
});
await addStepsToFirestore(totalSteps);
} else {}
}
@override
void initState() {
super.initState();
health.requestAuthorization(types);
getSteps();
getCaloriesBurned();
fetchPreviousSteps();
fetchPreviousCalories();
}
Let me know if you any more information. Thanks once again.
Here are screenshots of my iPhone’s permissions too:
I thought it would work exactly as the Simulator would, but it does not.