我想在我的iOS应用中访问HK。我已经完成了所有设置,或者我认为是正确的。但是当它运行时,我得到一个“ - [__ NSArrayI _allowAuthorizationForReadingWithEntitlements:]:无法识别的选择器发送到实例0x7f99badc54f0”错误,我不知道为什么。我跟着Ray Wenderlichs的帖子,甚至当我重新授权并运行它时,他的应用程序也无法运行。
这是我的代码,以防任何人有任何想法,我已经尝试通过调试,无法弄明白
import Foundation
import HealthKit
class HealthManager {
let healthKitStore : HKHealthStore = HKHealthStore()
func authorizeHealthKit(completion: ((success:Bool, error:NSError!) -> Void)!) {
// What we want to read
let healthKitTypesToRead = Set(arrayLiteral:[
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex),
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth),
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex),
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight),
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning),
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned),
HKObjectType.workoutType()
])
// What we want to write
let healthKitTypesToWrite = Set(arrayLiteral: [
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex),
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth),
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex),
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight),
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning),
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned),
HKObjectType.workoutType()
])
// Checking if healthkit is available
if !HKHealthStore.isHealthDataAvailable() {
let error = NSError(domain: "com.mpc.Health", code: 2, userInfo: [NSLocalizedDescriptionKey:"HealthKit is not available in this Device"])
if( completion != nil )
{
completion(success:false, error:error)
}
return;
}
//Requesting the authorization
healthKitStore.requestAuthorizationToShareTypes(nil, readTypes: healthKitTypesToRead) { (success, error) -> Void in
if( completion != nil )
{
completion(success:success,error:error)
}
}
}
}
答案 0 :(得分:3)
对于healthKitTypesToRead和healthKitTypesToWrite,您正在创建一个包含HKObjectTypes数组的NSSet。它应该是没有数组的HKObjectTypes的NSSet。
另请注意,您的代码段未使用healthKitTypesToWrite。但如果是,则该集合不应包含任何HKCharacteristicTypes。 HealthKit不允许应用程序修改用户特征,因此您无法请求授权来编写这些类型(如果您尝试,则会抛出异常)。
答案 1 :(得分:2)
万一有人在寻找代码,以下内容对我有用
let healthKitTypesToRead = Set(arrayLiteral:
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!,
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
HKObjectType.workoutType()
)