rag*_*oat 9 ios swift healthkit watchkit
在我的手表扩展中,我称之为此功能:
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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
在AppDelegate.swift中,我有:
func applicationShouldRequestHealthAuthorization(_ application: UIApplication) {
let healthStore = HKHealthStore()
healthStore.handleAuthorizationForExtension { (success, error) -> Void in
//...
}
}
Run Code Online (Sandbox Code Playgroud)
我在手表和手机上显示对话框,当我从对话框中告诉它时,它会在手机上打开应用程序.我遇到的问题是手机应用程序不显示应该显示的权限表以允许权限.这里提到了权限表:https: //developer.apple.com/reference/healthkit
我需要做些什么才能让它出现以便授予权限?现在,我可以通过访问Health应用程序来获取权限,然后选择我的应用程序并以这种方式授予权限.
编辑:我从requestAuthorization()方法调用打印出此错误.
error Optional("Required usage description strings not present in app\'s Info.plist")
Run Code Online (Sandbox Code Playgroud)
有了这个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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
976 次 |
| 最近记录: |