HKHealthStore requestAuthorizationToShareTypes:readTypes:completion总是返回成功?

时间:2014-08-20 08:29:54

标签: ios ios8 health-kit

我似乎无法捕获和处理用户取消HealthKit权限。完成块始终返回成功,并且nil Error。

[[DataManager healthStore] requestAuthorizationToShareTypes:dataTypesToWrite
                                                  readTypes:dataTypesToRead
                                                 completion:^(BOOL success, NSError *error)
 {
     if (!success)
     {
         NSLog(@"You didn't allow HealthKit to access these read/write data types. In your app, try to handle this error gracefully when a user decides not to provide access. The error was: %@. If you're using a simulator, try it on a device.", error);
     }

     if (completion)
     {
         completion(success,error);
     }
 }];

3 个答案:

答案 0 :(得分:4)

success参数不表示您的应用是否被授予了所请求的授权。它仅指示是否成功提示用户进行授权(如果需要提示)。如果用户取消提示或由于某种原因无法显示提示,则成功参数为NO,错误将为非零。

另请注意,根据设计,无法查询应用程序是否具有类型的读取权限。您只能使用 - [HKHealthStore authorizationStatusForType:] API查询应用程序是否有权保存类型的对象。

答案 1 :(得分:2)

这确实是一个错误。它已在iOS 8 Golden Master种子中修复。

答案 2 :(得分:-1)

如果您想知道是否为healthkit提供了任何权限,可以使用此方法。

-(BOOL) anyPermissionIsGiven {

NSMutableArray * authArray = [[NSMutableArray alloc]init];

[authArray addObject:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietarySodium]];
[authArray addObject:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCarbohydrates]];
[authArray addObject:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFiber]];
[authArray addObject:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed]];
[authArray addObject:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryProtein]];
[authArray addObject:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminA]];
[authArray addObject:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminB6]];
[authArray addObject:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminC]];
[authArray addObject:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminE]];
[authArray addObject:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminK]];
[authArray addObject:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCalcium]];
[authArray addObject:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryThiamin]];
[authArray addObject:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFolate]];
[authArray addObject:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryPhosphorus]];
[authArray addObject:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryMagnesium]];
[authArray addObject:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryPotassium]];

BOOL anyPermissionGiven = NO;
for (HKQuantityType * quantityType in authArray) {
    if([self.healthStore authorizationStatusForType:quantityType] == HKAuthorizationStatusSharingAuthorized) {
        anyPermissionGiven = YES;
        break;
    }
}

return anyPermissionGiven;

}