如何检查 HealthKit 中是不是允许读取步骤权限?
Posted
技术标签:
【中文标题】如何检查 HealthKit 中是不是允许读取步骤权限?【英文标题】: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];
];
现在它显示屏幕要求读取步骤的权限。但是当我尝试检查权限状态时。它显示权限被拒绝。我如何检查权限是允许还是拒绝。
【问题讨论】:
***.com/questions/33018445/… HealthKit Authorisation Status is always 1的可能重复 根据设计,无法检查您的应用是否有权从 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)
)
【讨论】:
只有当你同时获得读写权限时才会给出完美的结果,如果我们只获得读取权限,我们无法获得用户是否允许读取权限的状态以上是关于如何检查 HealthKit 中是不是允许读取步骤权限?的主要内容,如果未能解决你的问题,请参考以下文章
HealthKit:不允许读取 HKCorrelationType
HealthKit:不允许读取 HKCorrelationType