簡體   English   中英

Swift HealthKit更新生日

[英]Swift HealthKit update Birthday

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

這是我的授權函子:

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

但是,如何以編程方式更改/更新生日?

感謝幫助!

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

文檔中

HKCharacteristicType類是HKObjectType類的具體子類。 HealthKit使用特征類型來表示通常不會隨時間變化的數據。 與其他對象類型不同,特征類型不能用於創建新的HealthKit對象。 相反,用戶必須使用“健康狀況”應用程序輸入和編輯其特征數據。 僅當請求允許從HealthKit存儲讀取數據時才使用特征類型。

HealthKit框架參考;

https://developer.apple.com/library/ios/documentation/HealthKit/Reference/HealthKit_Framework/index.html#//apple_ref/doc/uid/TP40014707

HealthKit對象可以分為兩個主要組:特征和樣本。 特征對象表示通常不變的數據。 該數據包括用戶的生日,血型和生物性別。 您的應用程序無法保存特征數據。 用戶必須使用“運行狀況”應用程序輸入或修改此數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM