This video provides a basic introduction to HealthKit.
- [Instructor] What is HealthKit? HealthKit is a framework on iOS. It stores health and fitness data in a central location. It takes in data from multiple sources, which could be different devices. It allows users to control access to their data. And maintains privacy of user data. HealthKit works on iPhone and on Apple Watch. At this time it does not work on iPad. Each device has its own data store, but data is synced between the two. So your phone and your watch will sync data between the two devices.
Some HealthKit data includes characteristic data such as birth date, blood type and other things that don't really change. Sample data which is data collected over a period of time such as a heart rate, energy burned, distance or other things that you would track during a workout. Source data includes which device the data came from or which application. HealthKit also retains access to some deleted objects which you can query for. The two HealthKit data types that we're going to look at for this session are Heart Rate and Workout.
To get out a heartRateType you would use HKQuantityType.quantityType forIdentifier, and pass in a heartRate. Workouts are a little bit easier. You just use an HKObjectType.workoutType() The first steps to creating a HealthKit app are to create an iOS app with WatchKit app in Xcode. Then we're going to add app icons for the phone and the watch. We're going to enable HealthKit capabilities in our app. Then we're going to update the Info.plist with the description strings that HealthKit requires.
Finally we're going to authorize HealthKit access. The two strings in the info.plist that we're required to supply are an NSHealthShareUsageDescription which describes the reason why this app should read user's health data. An NSHealthUpdateUsageDescription which describes the reason why your app should be able to make changes to health data. You should make sure to create a description so that your user understands why you're asking for read and write access to their health data.
Don't worry if you're getting confused. We'll go over this in our next video. When you ask for HealthKit access the HealthKit framework is going to present some prompts to allow users to allow or deny access to their healthcare. On the watch it'll prompt you to adjust your settings in the Workout Tracker app on your iPhone. On the phone you'll see an alert to open the app that's requesting access to your health data. Once you open it it'll present the health access screen which allows you to turn on or off access to each specific data type that the app is requesting access to.
Don't forget if you're going to be running your app on a device that you need to go to developer.apple.com and in your App IDs you need to enable HealthKit access for your app. Okay great, let's get started.
- Creating a HealthKit-enabled project
- Authorizing HealthKit on an Apple Watch and iPhone
- Creating a workout on an Apple Watch
- Creating workout sessions
- Saving a workout to HealthKit
- Creating a query
- Displaying workout data on an iPhone
- Creating an Activity summary query
- Displaying an Activity ring on an Apple Watch and iPhone