Reputation: 1003
I am trying to access HK in my iOS app. I have everything set up, or so I thought, properly. But when it runs I get a "-[__NSArrayI _allowAuthorizationForReadingWithEntitlements:]: unrecognized selector sent to instance 0x7f99badc54f0" error and I'm not sure why. I followed Ray Wenderlichs post, and even his app isn't working when I re-entitle it and run it.
Here's my code in case anyone has any idea, I've tried looking through the debug and can't figure it out
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)
}
}
}
}
Upvotes: 3
Views: 819
Reputation: 206
Just in case anyone is looking for code, the following worked for me
let healthKitTypesToRead = Set(arrayLiteral:
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!,
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
HKObjectType.workoutType()
)
Upvotes: 2
Reputation: 7363
For both healthKitTypesToRead and healthKitTypesToWrite, you are creating an NSSet that contains an array of HKObjectTypes. It should be an NSSet of HKObjectTypes without the array.
Also note that your code snippet is not using healthKitTypesToWrite. If it were, though, that set should not contain any HKCharacteristicTypes. HealthKit does not allow apps to modify user characteristics so you cannot request authorization to write those types (an exception will be thrown if you try).
Upvotes: 3