在我的 watch 扩展中,我调用了这个函数:
func requestAuthorization() {
let healthStore = HKHealthStore()
let workoutType = HKObjectType.workoutType()
let heartRateType = HKObjectType.quantityType(forIdentifier: .heartRate)
//reading
let readingTypes = Set([heartRateType!, workoutType])
//writing
let writingTypes = Set([heartRateType!, workoutType])
//auth request
healthStore.requestAuthorization(toShare: writingTypes, read: readingTypes) { (success, error) -> Void in
if error != nil {
print("error \(error?.localizedDescription)")
} else if success {
self.startButton.setEnabled(true)
} else if !success {
self.startButton.setEnabled(false)
}
}
}
在 AppDelegate.swift 中,我有:
func applicationShouldRequestHealthAuthorization(_ application: UIApplication) {
let healthStore = HKHealthStore()
healthStore.handleAuthorizationForExtension { (success, error) -> Void in
//...
}
}
我在 watch 和手机上看到了对话框,当我从对话框中告诉它时,它会在手机上打开应用程序。我遇到的问题是电话应用程序没有显示应该显示以允许权限的权限表。此处提到了许可表: https://developer.apple.com/reference/healthkit
我需要做什么才能让它出现以便授予权限?就目前而言,我可以通过转到“健康”应用程序然后获取并选择我的应用程序并以这种方式授予权限来授予权限。
编辑:我从 requestAuthorization() 方法调用中打印出此错误。
error Optional("Required usage description strings not present in app\'s Info.plist")
最佳答案
有了这个TestHealthKit ,我能够打开 health-kit 许可表。
// 4. Request HealthKit authorization
(HealthStore as! HKHealthStore).requestAuthorizationToShareTypes(nil, readTypes: dataTypesToRead()) { (success, error) -> Void in
// self.CheckAuthorizationStatusFor(self.dataTypesToRead())
if (success)
{
SuccessCompletion(success: success)
return;
}
else
{
FailureCompletion(err: error)
return;
}
}
关于ios - HealthKit 许可表未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40777457/