From 2178a8ed09eada555de35855d68144aa37e30263 Mon Sep 17 00:00:00 2001 From: Evgenii Evstropov Date: Fri, 11 May 2018 22:06:20 +0200 Subject: [PATCH] add function to check share(write) permissions authorisation --- .../RCTAppleHealthKit+TypesAndPermissions.h | 2 ++ .../RCTAppleHealthKit+TypesAndPermissions.m | 1 - RCTAppleHealthKit/RCTAppleHealthKit.h | 1 + RCTAppleHealthKit/RCTAppleHealthKit.m | 22 +++++++++++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.h b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.h index 73438fe..f8e3047 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.h @@ -10,6 +10,8 @@ @interface RCTAppleHealthKit (TypesAndPermissions) +- (NSDictionary *)readPermsDict; +- (NSDictionary *)writePermsDict; - (NSSet *)getReadPermsFromOptions:(NSArray *)options; - (NSSet *)getWritePermsFromOptions:(NSArray *)options; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m index 7022081..3d43bad 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m @@ -83,7 +83,6 @@ return writePerms; } - // Returns HealthKit read permissions from options array - (NSSet *)getReadPermsFromOptions:(NSArray *)options { NSDictionary *readPermDict = [self readPermsDict]; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.h b/RCTAppleHealthKit/RCTAppleHealthKit.h index 900c6e3..eadffd3 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit.h @@ -19,6 +19,7 @@ - (void)isHealthKitAvailable:(RCTResponseSenderBlock)callback; - (void)initializeHealthKit:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)checkPermission:(NSString *)input callback:(RCTResponseSenderBlock)callback; - (void)getModuleInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index bbc9c17..d26a708 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -36,6 +36,11 @@ RCT_EXPORT_METHOD(initHealthKit:(NSDictionary *)input callback:(RCTResponseSende [self initializeHealthKit:input callback:callback]; } +RCT_EXPORT_METHOD(checkSharePermission:(NSString *)input callback:(RCTResponseSenderBlock)callback) +{ + [self checkPermission:input callback:callback]; +} + RCT_EXPORT_METHOD(initStepCountObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { [self fitness_initializeStepEventObserver:input callback:callback]; @@ -248,6 +253,23 @@ RCT_EXPORT_METHOD(saveMindfulSession:(NSDictionary *)input callback:(RCTResponse } } +- (void)checkPermission:(NSString *)input callback:(RCTResponseSenderBlock)callback +{ + self.healthStore = [[HKHealthStore alloc] init]; + if ([HKHealthStore isHealthDataAvailable]) { + + HKObjectType *val = [[self writePermsDict] objectForKey:input]; + + if ([self.healthStore authorizationStatusForType:val] == HKAuthorizationStatusSharingAuthorized) { + callback(@[[NSNull null], @true]); + } else { + callback(@[[NSNull null], @false]); + } + } else { + callback(@[RCTMakeError(@"HealthKit data is not available", nil, nil)]); + } +} + - (void)getModuleInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { NSDictionary *info = @{ -- 2.26.2