iOS:为HealthKit,DOB,Height等开发为零

时间:2015-03-25 16:52:21

标签: ios swift health-kit

所以,我的任务是学习HealthKit,看起来相当直接。开始构建我自己的应用程序,可以获得步骤等但是我无法获得出生日期,性别等。他们总是以零返回

然后我下载了Apple的示例应用程序Fit,再次没有回来。

我已授权这两个应用并创建了健康医疗ID 我位于英国?这是我们唯一的事吗?

func readProfile() -> ( age:Int?,  biologicalsex:HKBiologicalSexObject?, bloodtype:HKBloodTypeObject?)
  {
    var error:NSError?
    var age:Int?

    // 1. Request birthday and calculate age
    if let birthDay = healthKitStore.dateOfBirthWithError(&error)
    {
      let today = NSDate()
      let calendar = NSCalendar.currentCalendar()
      let differenceComponents = NSCalendar.currentCalendar().components(.YearCalendarUnit, fromDate: birthDay, toDate: today, options: NSCalendarOptions(0) )
      age = differenceComponents.year
    }
    if error != nil {
      println("Error reading Birthday: \(error)")
    }

    // 2. Read biological sex
    var biologicalSex:HKBiologicalSexObject? = healthKitStore.biologicalSexWithError(&error);
    if error != nil {
      println("Error reading Biological Sex: \(error)")
    }
    // 3. Read blood type
    var bloodType:HKBloodTypeObject? = healthKitStore.bloodTypeWithError(&error);
    if error != nil {
      println("Error reading Blood Type: \(error)")
    }

    // 4. Return the information read in a tuple
    return (age, biologicalSex, bloodType)
  }

1 个答案:

答案 0 :(得分:2)

配置医疗ID不会填充任何HealthKit类型的值。尝试使用Health应用程序中的Health Data选项卡为每种类型设置值。