0

I am trying to read steps from Apple Health application but unable to find that user allows permission or denied. Here is my code:-

NSArray *readTypes = @[[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]];


HKAuthorizationStatus permissionStatus = [self.healthStore authorizationStatusForType:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]];

if (permissionStatus == HKAuthorizationStatusSharingAuthorized) {
    return ;
}
else{
    [self.healthStore  requestAuthorizationToShareTypes:[NSSet setWithArray:@[]] readTypes:[NSSet setWithArray:readTypes] completion:^(BOOL success, NSError * _Nullable error) {
        NSLog(@"%@",error);

        [[NSNotificationCenter defaultCenter] postNotificationName:kConnectToAppleHealthNotification object:nil];

    }];
}

Now it shows screen to ask permission to read steps.but when I am trying to check permission status. it shows permission denied. How I can check permission is allow or denied.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Vikash Dummy
  • 13
  • 1
  • 7
  • http://stackoverflow.com/questions/33018445/how-to-check-if-healthkit-has-been-authorized –  May 15 '17 at 12:51
  • Possible duplicate of [HealthKit Authorisation Status is always 1](http://stackoverflow.com/questions/29076655/healthkit-authorisation-status-is-always-1) – Allan May 16 '17 at 16:17
  • There is no way to check whether your app is authorized to read data from HealthKit, by design. See the answer above for more details. – Allan May 16 '17 at 16:18

2 Answers2

0

Yes Vikash is right I have checked it by the below code:

// Create the query
HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:quantityType
    quantitySamplePredicate:nil
    options:HKStatisticsOptionCumulativeSum
    anchorDate:anchorDate
    intervalComponents:interval];

// Set the results handler
query.initialResultsHandler = ^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *results, NSError *error) {

if (error) {
    // Perform proper error handling here
    NSLog(@"*** An error occurred while calculating the statistics: %@ ***",error.localizedDescription);
}

[results enumerateStatisticsFromDate:startDate
    toDate:endDate
    withBlock:^(HKStatistics *result, BOOL *stop) {

    DLog(@" Result source %@ ",result.sources);
    if(!result.sources){

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"You need to give us permission for getting your health data, Otherwise you can't get benefit of this app." preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionYes = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: UIApplicationOpenSettingsURLString]];

    }];
    [alertController addAction:actionYes];

    UIAlertAction *actionNo = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    delegate.window.userInteractionEnabled = NO;

    }];
    [alertController addAction:actionNo];
    [delegate.window.rootViewController  presentViewController:alertController animated:YES completion:nil];


    return ;
 }

here you can continue your code which you want to do with health kit data.

chirag90
  • 2,211
  • 1
  • 22
  • 37
Chandni
  • 692
  • 1
  • 10
  • 25
-2
Swift Version :

Here an example of requesting and checking permission access in HealthKitStore
// Present user with items we need permission for in HealthKit

healthKitStore.requestAuthorization(toShare: typesToShare, read: typesToRead, completion: { (userWasShownPermissionView, error) in

    // Determine if the user saw the permission view
    if (userWasShownPermissionView) {
        print("User was shown permission view")

        // ** IMPORTANT
        // Check for access to your HealthKit Type(s). This is an example of using BodyMass.
        if (self.healthKitStore.authorizationStatus(for: HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)!) == .sharingAuthorized) {
            print("Permission Granted to Access BodyMass")
        } else {
            print("Permission Denied to Access BodyMass")
        }

    } else {
        print("User was not shown permission view")

        // An error occurred
        if let e = error {
            print(e)
        }
    }
})
  • 2
    this will give perfect result only when you take both read and write permission, if we only take read permission we can't get status that user allow read permission or not – Hardik Thakkar Nov 15 '18 at 09:03