嘿,让我们直达重点
问题是用户拒绝健康包的许可权时,先使用requestAuthorization
询问,然后稍后从设置中授予许可权,但是authorizationStatus
({ {3}}返回 sharingdenied (即使共享已授予许可)
复制步骤
添加以下内容以请求许可,然后按“不允许”
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)
}
现在打开Health Kit应用程序(在“源”选项卡中)并允许所有权限
返回应用程序和
添加以下代码
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)")
}
}
您将看到其中一些值是 Authorized (已授权),但大多数都是 Sharing Denied (共享被拒绝)。但是,您可以从Health Kit中读取数据
请建议