Привет, давайте перейдем прямо к точке
Проблема в том, когда пользователь Отказал в разрешении для аптечки, используя requestAuthorization
, а затем предоставил разрешение из настроек но authorizationStatus
( Doc) возврат совместное использование запрещено даже при наличии разрешения
Шаги для воспроизведения
Добавьте следующее, чтобы запросить разрешение, и нажмитене разрешать
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)
}
Теперь откройте приложение аптечки (на вкладке источника) и разрешите все разрешения
назад к приложению и
добавьте следующий код
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)")
}
}
Вы увидите, что некоторые значения Авторизованы , но большинство из них Отказ в обмене Однако вы можете прочитать данные из аптечки
Пожалуйста, предложите