我正在尝试从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];
}];
}
现在它显示要求读取步骤权限的屏幕。但是当我尝试检查权限状态时。它显示许可被拒绝。如何允许或拒绝检查权限。
答案 0 :(得分:0)
是的,Vikash是对的 我已通过以下代码检查了它:
// 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 ;
}
在这里,您可以继续使用健康套件数据执行的代码。
答案 1 :(得分:-1)
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)
}
}
})