2

HealthKit の権限をキャンセルするユーザーをキャプチャして処理できないようです。完了ブロックは常に成功と nil エラーを返します。

[[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);
     }
 }];
4

3 に答える 3

4

The success parameter does not indicate whether your app was granted the authorizations that were requested. It only indicates whether the user was successfully prompted for authorization (if prompting was necessary). The success parameter will be NO and the error will be non-nil if the user cancels the prompt or the prompt could not be shown for some reason.

また、設計上、アプリケーションが型の読み取りアクセス権を持っているかどうかを照会する方法がないことに注意してください。-[HKHealthStore authorizationStatusForType:] APIを使用して、アプリケーションが特定のタイプのオブジェクトを保存することを許可されているかどうかのみを照会できます。

于 2014-08-30T20:41:48.633 に答える
2

これは確かにバグでした。iOS 8 ゴールデン マスター シードで修正されました。

于 2014-09-09T22:12:54.677 に答える
-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;

}

于 2016-06-28T07:16:59.883 に答える