[英]How to update UI and ask Permission for Healthkit
Nowadays I am working on an app related to Healthkit in iOS.现在我正在开发一个与 iOS 中的 Healthkit 相关的应用程序。 I am facing a problem.
我正面临一个问题。 In the first view controller, which is HomeVC, when I tried to ask for healthkit permission, it's working fine, and show a system screen for permission.
在第一个视图控制器 HomeVC 中,当我尝试请求 healthkit 权限时,它工作正常,并显示系统屏幕以获取权限。
But Meanwhile, I have to setup the layout of UI elements on viewcontoller with asking permission, So I added some code to update UI in viewDidload method.但是同时,我必须在征得许可的情况下在 viewcontoller 上设置 UI 元素的布局,所以我在 viewDidload 方法中添加了一些代码来更新 UI。 In that case when I ask permission, the permission screen not showing and showing white screen, and following error in the console.
在这种情况下,当我请求许可时,许可屏幕没有显示并显示白屏,并且在控制台中出现以下错误。
Error Domain=com.apple.healthkit Code=100 "Authorization session timed out" UserInfo={NSLocalizedDescription=Authorization session timed out}
错误域=com.apple.healthkit 代码=100“授权会话超时” UserInfo={NSLocalizedDescription=授权会话超时}
Is there any way to solve this?有没有办法解决这个问题?
Add into info.plist file.添加到 info.plist 文件中。 Add following Information property List -> Privacy - Health Update Usage Description Description -> App wants to access your health kit to provide you better fitness result
添加以下信息属性列表->隐私-健康更新使用说明描述->应用程序想要访问您的健康包以提供更好的健身效果
func authorizeHealthKit() {
let healthKitTypesToRead : Set<HKObjectType> = [
HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.dateOfBirth)!,
HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.bloodType)!,
HKObjectType.quantityType(forIdentifier: .stepCount)!,
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!
]
let healthKitTypes: Set = [ HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!, HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.activeEnergyBurned)! ]
let healthKitTypesToWrite : Set<HKSampleType> = []
if !HKHealthStore.isHealthDataAvailable() {
print("error")
return
}
self.healthKitStore.requestAuthorization(toShare: healthKitTypesToWrite, read: healthKitTypesToRead) { (success, error) in
print("request authorized succesfully")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.