首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用HealthKit的异常

使用HealthKit的异常
EN

Stack Overflow用户
提问于 2015-06-30 21:34:40
回答 1查看 534关注 0票数 2

我使用Xcode来用HealthKit创建一个应用程序,但是每当我试图在iOS模拟器中授权HealthKit时,它就会崩溃。我的密码在底部。有人知道怎么解决这个问题吗?

代码语言:javascript
复制
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: "com.TestHKTutorial", 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)
  }
}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-02 17:32:05

healthKitTypesToRead和healthKitTypesToWrite集包含HKObjectTypes数组,而不是仅包含HKObjectTypes。试一试:

代码语言:javascript
复制
let healthKitTypesToRead : Set = [
    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 = [
    HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex)!,
    HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
    HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!,
    HKQuantityType.workoutType()
]
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31148927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档