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

Byt*_*yte 4 ios swift healthkit hkhealthstore

我在我的应用程序中使用AppleHealthKit.一切都正常.问题是我无法检测用户在请求许可时是否点击"不允许"按钮.

在此输入图像描述

使用此方法,我的应用程序使用HealthKit,即使用户不允许这样做.

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

Apple文档:

在此输入图像描述

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

Sul*_*han 6

您无法通过设计检测到它:

为了帮助防止敏感健康信息可能泄漏,您的应用无法确定用户是否已授予读取数据的权限.如果您未获得许可,则看起来好像HealthKit商店中没有所请求类型的数据.

(来自HKHealthStore)


Mrs*_*eam 5

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

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");
Run Code Online (Sandbox Code Playgroud)