HealthKit 在 Swift 2.0 中仅启动一次

HealthKit is launching only once in Swift 2.0

我已经将 HealthKit 框架集成到我的应用程序中。 HealthKit 仅从下面的 application.The 启动一次代码位于为 HealthKit 创建的单例 class 中。

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 正在从 viewcontroller、

调用按钮操作
@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")
                }
            }

你应该能看出区别。