如何检测用户是否不允许 Apple Health Kit 授权?

How to detect if the user doesn't allow Apple Health Kit Authorisation?

我在我的应用程序中使用 AppleHealthKit。一切正常。问题是我无法检测到用户在请求许可时是否点击了 "Don't Allow" 按钮。

通过这种方法,我的应用程序使用 HealthKit,即使用户不允许这样做。

requestAuthorizationToShareTypes(healthKitTypesToWrite, readTypes: healthKitTypesToRead) { (success, error) -> Void in
        if( completion != nil ) {
            completion(success:success,error:error)
        }

Apple 文档:

所以基本上我的问题是如何检测到这个?

设计使您无法检测到它:

To help prevent possible leaks of sensitive health information, your app cannot determine whether a user has granted permission to read data. If you are not given permission, it simply appears as if there is no data of the requested type in the HealthKit store.

(来自 HKHealthStore

您可以这样做,但仅限于一种特定类型且仅限于写入数据(如果您必须检查它们,您也可以对其他类型执行相同操作)

HKHealthStore *_healthStore;
HKQuantityType *stepsType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

HKAuthorizationStatus status = [_healthStore authorizationStatusForType:stepsType];

BOOL isActive;
switch (status) {
    case HKAuthorizationStatusSharingAuthorized:
        isActive = YES;
        break;
    case HKAuthorizationStatusSharingDenied:
        isActive = NO;
        break;
    case HKAuthorizationStatusNotDetermined:
        isActive = NO;
        break;

    default:
        break;
}

NSLog(@"STATUS-------%@", isActive ? @"yes" : @"no");