[英]HealthKit Auth in Swift
I've been building a basic app to collect fitness and other data from health kit in swift, however, I'm having an issue getting authorization to occur 我一直在构建一个基本的应用程序以快速从健康套件中收集健身和其他数据,但是,在获得授权时我遇到了问题
class HealthManager {
let healthKitStore:HKHealthStore = HKHealthStore()
func authorizeHealthKit(completion: ((success:Bool, error:NSError!) -> Void)!)
{
// 1. Set the types you want to read from HK Store
let healthKitTypesToRead = Set(arrayLiteral:[
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth),
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBloodType),
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex),
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass),
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight),
HKObjectType.workoutType()
])
// 2. Set the types you want to write to HK Store
let healthKitTypesToWrite = Set(arrayLiteral:[
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex),
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned),
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning),
HKQuantityType.workoutType()
])
// 3. If the store is not available (for instance, iPad) return an error and don't go on.
if HKHealthStore.isHealthDataAvailable()
{
let error = NSError(domain: "me.kebabman.healthkit", code: 2, userInfo: [NSLocalizedDescriptionKey:"HealthKit is not available in this Device"])
if( completion != nil )
{
completion(success:false, error:error)
}
return;
}
// 4. Request HealthKit authorization
healthKitStore.requestAuthorizationToShareTypes(healthKitTypesToWrite, readTypes: healthKitTypesToRead) { (success, error) -> Void in
if( completion != nil )
{
completion(success:success,error:error)
}
}
}
}
from another class (my view controller) i'm calling the authoriseHealthKit func - no matter what i change i'm always getting back HealthKit is not available on this device (using the iOS 8.3 simulator) 从另一个类(我的视图控制器)开始,我正在调用authoriseHealthKit函数-不管我做了什么更改,我总是会回来HealthKit在此设备上不可用(使用iOS 8.3模拟器)
From reading through the forums this should be working however I can't figure out why. 通过阅读论坛,这应该有效,但是我不知道为什么。
Thanks 谢谢
Your IF statement is wrong 您的IF陈述有误
This section of code if HKHealthStore.isHealthDataAvailable() { return error ... }
这部分代码,
if HKHealthStore.isHealthDataAvailable() { return error ... }
should be 应该
if !HKHealthStore.isHealthDataAvailable() { return error ... }
or 要么
if HKHealthStore.isHealthDataAvailable() { request authorization ... } else { return error... }
Did you add the entitlements? 您是否添加了权利? Check this answer: Try HealthKit with the iOS Simulator without developer account .
检查以下答案: 在没有开发人员帐户的情况下,使用带有iOS模拟器的HealthKit进行尝试 。 That's most likely your problem.
这很可能是您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.