Skip to content

Commit 83437c1

Browse files
authored
Merge pull request #91 from jevonmao/fix_healthkit_deny_#90
Fix bug of HealthKit button show incorrect deny from #90
2 parents ca59ea3 + 25d62e7 commit 83437c1

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

Sources/PermissionsSwiftUIHealth/JMHealthPermissionManager.swift

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ import HealthKit
1212

1313
@available(iOS 13.0, tvOS 13.0, *)
1414
public extension PermissionType.PermissionManager {
15+
/**
16+
Permission that allows app to access healthkit information
17+
18+
- Note: Extensive Info.plist values and configurations are required for HealthKit authorization. Please see Apple Developer [website](https://developer.apple.com/documentation/healthkit/authorizing_access_to_health_data) for details. \n
19+
20+
For example, passing in a `Set` of `HKSampleType`:
21+
```
22+
[.health(categories: .init(readAndWrite: Set([HKSampleType.quantityType(forIdentifier: .activeEnergyBurned)!])))]
23+
```
24+
25+
- Attention: From Apple Developer Documentation: "to help prevent possible leaks of sensitive health information, your app cannot determine whether or not 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."
26+
*/
1527
static func health(categories: HKAccess) -> JMHealthPermissionManager {
1628
JMHealthPermissionManager(categories: categories)
1729
}
@@ -33,46 +45,50 @@ public class JMHealthPermissionManager: PermissionType.PermissionManager {
3345
init(categories: HKAccess) {
3446
self.categories = categories
3547
}
48+
49+
/**
50+
- Note: From Apple Developer Documentation: "to help prevent possible leaks of sensitive health information, your app cannot determine whether or not 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."
51+
*/
3652
public override var authorizationStatus: AuthorizationStatus {
3753
get {
38-
//Count to track total # of permissions allowed and denied each
39-
var allowDenyCount: CountComparison = (authorized: 0, denied: 0)
54+
var allowDenyCount: CountComparison = (authorized: 0, denied: 0) //Tracks # of authorized and denied health categories
4055
var status: AuthorizationStatus {
56+
4157
//Set to notDetermined if all permissions are not determined
4258
if allowDenyCount.0 == 0 && allowDenyCount.1 == 0 {
4359
return .notDetermined
4460
}
45-
//Set to authorized if majority are authorized
46-
if allowDenyCount.0 > allowDenyCount.1 {
61+
62+
//Set to authorized if at least 1 type is authorized
63+
if allowDenyCount.0 > 0 {
4764
return .authorized
4865
}
49-
//Set to denied if majority are denied, or equal # of allowed and denied
50-
return .denied
66+
67+
//If all types are denied, set status to denied
68+
else {
69+
return .denied
70+
}
5171
}
5272

53-
/**
54-
- Note: From Apple Developer Documentation: "to help prevent possible leaks of sensitive health information, your app cannot determine whether or not 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."
55-
*/
56-
57-
var readPermissions = categories.readPermissions
58-
var writePermissions = categories.writePermissions
5973
//Map the authorization status, remove allowed and denied permissions from array.
6074
//Increase allowDenyCount as needed.
61-
mapPermissionAuthorizationStatus(for: &readPermissions, forCount: &allowDenyCount)
62-
mapPermissionAuthorizationStatus(for: &writePermissions, forCount: &allowDenyCount)
75+
mapPermissionAuthorizationStatus(for: categories.writePermissions, forCount: &allowDenyCount)
76+
77+
//Assume all read permissions are authorized, because Apple restrict app from determining read data
78+
if categories.writePermissions.isEmpty {
79+
allowDenyCount.0 += categories.readPermissions.count
80+
}
6381
return status
6482
}
6583

6684
}
67-
func mapPermissionAuthorizationStatus(for permissions: inout Set<HKSampleType>,
85+
func mapPermissionAuthorizationStatus(for permissions: Set<HKSampleType>,
6886
forCount allowDenyCount: inout CountComparison) {
6987
for sampleType in permissions {
7088
switch healthStore.authorizationStatus(for: sampleType){
7189
case .sharingAuthorized:
72-
permissions.remove(sampleType)
7390
allowDenyCount.0 += 1
7491
case .sharingDenied:
75-
permissions.remove(sampleType)
7692
allowDenyCount.1 += 1
7793
default:
7894
()

0 commit comments

Comments
 (0)