2014-09-22 31 views
0

我想更新Apple Health中的生日。但我不知道如何。Swift HealthKit更新生日

這是我的授權FUNC:

private func requestAuthorisationForHealthStore() { 

    let dataTypesToWrite = [ 
     HKCharacteristicType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth), 
     HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass), 
     HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight) 
    ] 
    let dataTypesToRead = [ 
     HKCharacteristicType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth), 
     HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass), 
     HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight) 
    ] 

    self.healthStore?.requestAuthorizationToShareTypes(NSSet(array: dataTypesToWrite), 
     readTypes: NSSet(array: dataTypesToRead), completion: { 
      (success, error) in 
      if success { println("User completed authorisation request.") } 
      else { println("The user cancelled the authorisation request. \(error)") } 
    }) 
} 

用於請求生日,我打電話給我的功能:

func requestAgeAndUpdate() { 

    var error: NSError? 
    let dob = self.healthStore?.dateOfBirthWithError(&error) 

    if error != nil { 
     println("There was an error requesting the date of birth: \(error)") 
     return 
    } 

    self.ageLabel.text = "\(dob)" 
} 

但我怎麼能更改/編程方式更新的生日?

感謝您的幫助!

回答

3

您不能以編程方式更改這些特徵。用戶必須通過Health App輸入這些數據。

documentation

的HKCharacteristicType類是 HKObjectType類的具體子類。 HealthKit使用特徵類型來表示 通常不會隨時間變化的數據。與其他對象 類型不同,特徵類型不能用於創建新的HealthKit 對象。相反,用戶必須使用Health應用程序輸入和編輯其特徵數據 。僅當詢問 以獲取從HealthKit存儲讀取數據的權限時,才使用特徵類型。