我正在尝试在我的 iOS 应用程序中访问 HK。我已经正确设置了所有内容,或者我认为是正确的。但是当它运行时,我得到一个“-[__NSArrayI _allowAuthorizationForReadingWithEntitlements:]: unrecognized selector sent to instance 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)
}
}
}
}