ios - 健康包权限返回错误值 ios Bug?
问题描述
嘿,让我们直奔主题
问题是当用户拒绝健康工具包的权限时询问使用requestAuthorization
,然后 从设置中授予权限, 但authorizationStatus
(文档)返回共享被拒绝, 即使授予权限
重现步骤
添加以下请求权限并按不允许
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)")
}
}
你会看到一些值是 授权的,但大部分是 拒绝共享但是你可以从健康工具包中读取数据
请建议
解决方案
推荐阅读
- node.js - 我试图在我的节点项目中使用 atom 命名文件“Distributor.js”
- python-3.x - Python 的 tkinter 窗口是否有 onload 事件?
- node.js - Console.log(err) 使我的网站崩溃
- reactjs - 未找到 Webpack-hot-middleware/client 模块
- sql-server - 在 SQL Server 中筛选日期时间列
- html - 是否可以只滚动一个 div,但不能滚动整个页面,除非您滚动到该 div 之外?
- proxy - 如何在 CAM 中设置代理服务器以从互联网上提取 CAM 模板?
- javascript - Gulp 不会覆盖 JS 文件
- c++ - 如何使用 Eigen 和 long double?
- python - 在熊猫数据框中查找特定部分字符串第一次出现的索引位置