【问题标题】:HealthKit is launching only once in Swift 2.0HealthKit 仅在 Swift 2.0 中启动一次
【发布时间】:2016-05-12 09:39:17
【问题描述】:

我已将 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 方法正在调用视图控制器的按钮操作,

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

    }

一旦我关闭了 healthkit 应用程序,按钮操作就不会发生任何操作。再次,如果我从模拟器中删除应用程序并单击按钮 healthkit 应用程序将启动。

谁能帮助我们上面的代码有什么问题。提前致谢。

【问题讨论】:

    标签: ios iphone swift healthkit


    【解决方案1】:

    如果授权已经被授予,应用程序将不会再次显示它。它只会直接调用成功处理程序。

    将您的 completion 处理程序更改为:

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

    你应该看到区别了。

    【讨论】:

    • 所以如果我想再次启动应用程序我应该如何继续
    • @suji 你没有做错什么。始终调用授权代码,但不要期望它向用户显示授权对话框。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多