【问题标题】:How to check read steps permission is allow or not in HealthKit?如何检查 HealthKit 中是否允许读取步骤权限?
【发布时间】:2017-05-15 12:46:05
【问题描述】:

我正在尝试从 Apple Health 应用程序中读取步骤,但找不到该用户允许或拒绝。这是我的代码:-

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];

    }];
}

现在它显示屏幕要求读取步骤的权限。但是当我尝试检查权限状态时。它显示权限被拒绝。我如何检查权限是允许还是拒绝。

【问题讨论】:

标签: ios objective-c healthkit


【解决方案1】:

是的,维卡什是对的 我已经通过以下代码检查了它:

// 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 ;
 }

您可以在此处继续您的代码,您想对健康工具包数据进行处理。

【讨论】:

    【解决方案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)
            }
        }
    })
    

    【讨论】:

    • 只有当你同时获得读写权限时才会给出完美的结果,如果我们只获得读取权限,我们无法获得用户是否允许读取权限的状态
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多