簡體   English   中英

HealthKit在Swift 2.0中僅啟動一次

[英]HealthKit is launching only once in Swift 2.0

我已經將HealthKit框架集成到我的應用程序中。 HealthKit僅從應用程序啟動一次。以下代碼在為HealthKit創建的單例類中。

func requestAuthorization()
        {

            if (HKHealthStore .isHealthDataAvailable() == false)
            {
                return
            }


            let healthKitTypesToRead : Set = [
                HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierFitzpatrickSkinType)!,
                HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!,
                HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBloodType)!
            ]

            let healthKitTypesToWrite : Set = [
                HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyFatPercentage)!,
                HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex)!,
                HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)!,
                HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!,
                HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierLeanBodyMass)!
            ]


                self.healthStore.requestAuthorizationToShareTypes(healthKitTypesToWrite, readTypes: healthKitTypesToRead) {
                    (success, error) -> Void in
                    if !success{
                        print("error")
                    }
                }
            }

方法requestAuthorization要求requestAuthorization按鈕操作,

@IBAction func healthIntegrationButton(sender: UIButton)
    {
        HealthKitHandler.shared.requestAuthorization()

    }

一旦我關閉了healthkit應用程序,該按鈕操作就不會發生任何操作。 再次,如果我從模擬器中刪除了該應用程序並單擊按鈕,則healthkit應用程序將啟動。

任何人都可以幫助我們上述代碼中的錯誤。 提前致謝。

如果授權已被授予,則該應用不會再次顯示。 它只會直接調用成功處理程序。

completion處理程序更改為:

self.healthStore.requestAuthorizationToShareTypes(healthKitTypesToWrite, readTypes: healthKitTypesToRead) {
                (success, error) -> Void in
                if success {
                    print("success!")
                }
                else {
                    print("error")
                }
            }

並且您應該看到區別。

暫無
暫無

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

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