3

Hey there let come straight to the point

Issue is When User Denied the permission for health kit ask using requestAuthorization and then later grant the permission from settings but authorizationStatus (Doc) return sharingdenied even though permission granted

Steps to reproduce

Add following to ask the permission and press don't allow

 class func authorizeHealthKit(completion: @escaping (Bool, Error?) -> Swift.Void) {

    guard HKHealthStore.isHealthDataAvailable() else {
        completion(false,HealthkitSetupError.notAvailableOnDevice)
        return
    }

    guard   let dateOfBirth = HKObjectType.characteristicType(forIdentifier: .dateOfBirth),
            let bloodType = HKObjectType.characteristicType(forIdentifier: .bloodType),
            let gender = HKObjectType.characteristicType(forIdentifier: .biologicalSex),

            let bodyMassIndex = HKObjectType.quantityType(forIdentifier: .bodyMassIndex),
            let height = HKObjectType.quantityType(forIdentifier: .height),
            let bodyMass = HKObjectType.quantityType(forIdentifier: .bodyMass),
            let activeEnergy = HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)


    else {
        completion(false,HealthkitSetupError.dataTypeNotAvailable)
        return
    }





    let healthKitTypesToWrite:Set<HKSampleType> = [bodyMassIndex,
                                                    activeEnergy,
                                                    HKObjectType.workoutType()
                                                    ]

    let healthKitTypesToRead:Set<HKObjectType> = [dateOfBirth,
                                                  bloodType,
                                                  gender,
                                                  height,
                                                  bodyMass,
                                                  HKSampleType.workoutType()

                                                    ]


    HKHealthStore().requestAuthorization(toShare: healthKitTypesToWrite, read: healthKitTypesToRead) { (granted, error) in
        completion(granted,error)
    }

Now Open health kit application (in source tab) and allow all permissions

back to application and

add following code

 let array = [HKObjectType.characteristicType(forIdentifier: .dateOfBirth),
                 HKObjectType.characteristicType(forIdentifier: .bloodType),
                 HKObjectType.characteristicType(forIdentifier: .biologicalSex),

                 HKObjectType.quantityType(forIdentifier: .bodyMassIndex),
                 HKObjectType.quantityType(forIdentifier: .height),
                 HKObjectType.quantityType(forIdentifier: .bodyMass),
                 HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)]


    for item in array {

        let type =  store.authorizationStatus(for: item!)

        switch type  {
        case .sharingDenied:
            print("Sharing Denied \(item)")
        case .notDetermined:
            print("Not Determined \(item)")
        case .sharingAuthorized:
            print("Authorized \(item)")

        }

    }

You will see some of the values is Authorized but most of are Sharing Denied However you can read data from health kit

Please suggest

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98

0 Answers0