VLDHealthKitManager
@interface VLDHealthKitManager : NSObject
VLDHealthKitManager is the Validic Mobile interface for all HealthKit operations. VLDHealthKitManager is a singleton object that must be accessed through the sharedInstance method. Like the other components of the Validic Mobile library, VLDHealthKitManager requires a valid user session in the VLDSession singleton.
VLDHealthKitManager can upload new records as they come in. Listening for new records is done by adding subscriptions for the desired sample types. This will automatically prompt the user for permission and setup background notifications for new data.
Background delivery will happen on an hourly basis if new data is available. This will result in the data being uploaded in the background throughout the day. For background delivery to be successful you must call observeCurrentSubscriptions from your application delegate’s application:didFinishLaunchingWithOptions: callback.
Notifications
When VLDHealthKitManager queues records for upload it will send an NSNotification. To listen for this
notification add an observer to the NSNotificationCenter with the constant kVLDHealthKitRecordsProcessedNotification
.
The userInfo dictionary for this notification will contain two keys, recordType
and count
. recordType will contain
an NSNumber representing the integer value of the VLDRecordType enum and count will contain the number of records that were
processed.
-
sharedInstance returns the VLDHealthKitManager singleton and should be the only way VLDHealthKitManager is accessed.
Declaration
Objective-C
+ (instancetype _Nonnull)sharedInstance;
Swift
class func sharedInstance() -> Self
-
Requests permission for and begins listening to HealthKit data for the specified subscriptions.
The added subscriptions are saved to the VLDSession object and persisted through the life of the session. The completion block is called once the permission screen has been dismissed or immediately if permission had been previously requested.
Declaration
Objective-C
- (void)setSubscriptionsFromIdentifiers: (NSArray<__kindof NSString *> *_Nullable)subscriptions completion:(nullable void (^)(void))completion;
Swift
func setSubscriptionsFromIdentifiers(_ subscriptions: [String]?, completion: (() -> Void)? = nil)
Parameters
subscriptions
An array of HKTypeIdentifiers VLDHealthKitManager to listen for and upload
completion
A completion block that gets called once authorization for access to the sample types is completed
-
Requests permission for and begins listening to HealthKit data for the specified subscriptions.
The added subscriptions are saved to the VLDSession object and persisted through the life of the session. The completion block is called once the permission screen has been dismissed or immediately if permission had been previously requested.
Declaration
Objective-C
- (void)setSubscriptions: (NSArray<__kindof HKSampleType *> *_Nullable)subscriptions completion:(nullable void (^)(void))completion;
Swift
func setSubscriptions(_ subscriptions: [HKSampleType]?, completion: (() -> Void)? = nil)
Parameters
subscriptions
An array of HKSampleType objects for VLDHealthKitManager to listen for and upload
completion
A completion block that gets called once authorization for access to the sample types is completed
-
Returns the list of HKSampleTypes that are currently being listened for.
Declaration
Objective-C
- (NSArray<__kindof HKSampleType *> *_Nullable)subscriptions;
Swift
func subscriptions() -> [HKSampleType]?
-
This restores the handlers for background delivery. This method must be called immediately when the app launches.
Declaration
Objective-C
- (void)observeCurrentSubscriptions;
Swift
func observeCurrentSubscriptions()
-
Fetch 6 months of historical records of the specified sets.
Declaration
Objective-C
- (void)fetchHistoricalSets: (NSArray<__kindof NSNumber *> *_Nonnull)historicalSets completion:(nullable void (^)(NSDictionary *_Nullable, NSError *_Nullable))completion;
Swift
func fetchHistoricalSets(_ historicalSets: [NSNumber], completion: (([AnyHashable : Any]?, Error?) -> Void)? = nil)
Parameters
historicalSets
The historical sets are specified as an NSArray of NSNumbers wrapping VLDHealthKitHistoricalSet enum values. Example:
@[@(VLDHealthKitHistoricalSetRoutine), @(VLDHealthKitHistoricalSetFitness)]
completion
Completion block is passed a dictionary with the count of records processed keyed by VLDRecordType enum wrapped as NSNumber. If an errors occurs, an NSError object is provided.
-
Queries HealthKit for the latest data.
Declaration
Objective-C
- (void)fetchLatestData;
Swift
func fetchLatestData()
-
Turns on processing intraday steps when steps are subscribed to in healthkit. This has to be set before calling observeCurrentSubscriptions.
Declaration
Objective-C
@property (nonatomic, assign, unsafe_unretained, readwrite) BOOL processIntraday;
Swift
var processIntraday: Bool { get set }