所以,我的任务是学习 HealthKit,看起来相当简单。开始构建我自己的应用程序,可以获得步骤等。但是我无法获得出生日期、性别等。它们总是返回为 nil
然后,我下载了 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)
}