Swift 1.2 健康工具包

Swift 1.2 HealthKit

我正在尝试在我的 iOS 应用程序中访问 HK。我已经把一切都设置好了,或者我认为是正确的。但是当它 运行s 我得到一个“-[__NSArrayI _allowAuthorizationForReadingWithEntitlements:]: unrecognized selector sent to instance 0x7f99badc54f0”错误,我不确定为什么。我关注了 Ray Wenderlichs post,当我重新授权它并 运行 它时,甚至他的应用程序也无法正常工作。

这是我的代码,以防有人有任何想法,我已经尝试查看调试但无法弄清楚

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)
        }
    }
}

}

对于 healthKitTypesToRead 和 healthKitTypesToWrite,您正在创建一个包含 HKObjectTypes 数组的 NSSet。它应该是没有数组的 HKObjectTypes 的 NSSet。

另请注意,您的代码片段未使用 healthKitTypesToWrite。但是,如果是,则该集合不应包含任何 HKCharacteristicTypes。 HealthKit 不允许应用程序修改用户特征,因此您不能请求授权来编写这些类型(如果您尝试,将抛出异常)。

以防万一有人在寻找代码,下面的代码对我有用

let healthKitTypesToRead = Set(arrayLiteral:
  HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!,
  HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth)!,
  HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex)!,
  HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)!,
  HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!,
  HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
  HKObjectType.workoutType()
  )