HOW TO: Enable HealthKit in your NativeScript application


HealthKit is framework that allows iOS apps that provide health and fitness services to share data. The user’s health information is stored in a centralized and secure location and every user decides which data should be available for your app. If your iOS app is designed to provide such services, the best way to do this is to use HealthKit.

There is an open source sample NativeScript app using HealthKit available based on this blog post.

HKit

Enabling the HealthKit

NOTE: This article assumes that you already have a NativeScript project with added iOS platform. You can check how to create NativeScript application here.

First thing to do in to turn on the HealthKit API in your project. To do so, open the .xcodeproj file that is located in the <MyProject>/platforms/ios folder in xCode. Then you can follow the steps that are described in the Adding Capabilities article

Health store

Once you have enabled the HealthKit, you can work with it like you use it in ObjC. The HealthKit store acts as your link to all the data managed by HealthKit. Use a HealthKit store to request permission to share or read HealthKit data. Once permission is granted, you can use the HealthKit store to save new samples to the store, or to manage the samples that your app has saved. Additionally, you can use the HealthKit store to start, stop, and manage queries.

Creating the store

You can create an instance of HKHealthStore with the following code:

 var healthStore = HKHealthStore.new();

Requesting permissions

To get access to the data, you must request permission from the user. The following code shows how to do this:

// Creating the quantity types.
var weightType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass);
 
// Choosing quantity types to write
var writeDataTypes = NSSet.setWithArray([weightType]);
 
// Choosing quantity types to read
var readDataTypes = NSSet.setWithArray([weightType]);
 
healthStore.requestAuthorizationToShareTypesReadTypesCompletion(writeDataTypes, readDataTypes, (success, error) {
}

 

Reading data

Once the user have granted permissions for you application, you are free access the requested data. To do so, you can use queries. The sample below shows how to execute a query in order to get the latest weight of the user: 

// Creating sort descriptors
var endDateSortDescriptor = NSSortDescriptor.alloc().initWithKeyAscending(HKSampleSortIdentifierEndDate, false);
 
// Creating the query
var query = HKSampleQuery.alloc().initWithSampleTypePredicateLimitSortDescriptorsResultsHandler(weightType, null, 1, [endDateSortDescriptor], (query, results, error) => {
   
}
 
// Executing the query
healthStore.executeQuery(query);

Writing data

Writing data to the HealthKit store is even easier:

// Creating the weight
var weight = HKQuantity.quantityWithUnitDoubleValue(HKUnit.poundUnit(), 83);
var now = NSDate.new();
var sample = HKQuantitySample.quantitySampleWithTypeQuantityStartDateEndDate(weightType, weight, now, now);
 
// Saving the weight
healthStore.saveObjectWithCompletion(sample,(success, error) => void {
});

Conclusion

And this is how to use HealthKit API in NativeScript. Yes, it is THAT simple.

To see the whole code app please visit the NativeScript framework open source repo. Please let us know what you think in the comments below or simply follow @NativeScript on twitter. We are open for ideas to create similar projects like this one to help you explore what is possible with {N}.

Author

Kamen Velikov

Comments


Comments are disabled in preview mode.
NativeScript is licensed under the Apache 2.0 license
© 2020 All Rights Reserved.