Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios HealthKit仅在Swift 2.0中发布一次_Ios_Iphone_Swift_Healthkit - Fatal编程技术网

Ios HealthKit仅在Swift 2.0中发布一次

Ios HealthKit仅在Swift 2.0中发布一次,ios,iphone,swift,healthkit,Ios,Iphone,Swift,Healthkit,我已将HealthKit框架集成到我的应用程序中。HealthKit仅从应用程序启动一次。以下代码位于为HealthKit创建的singleton类中 func requestAuthorization() { if (HKHealthStore .isHealthDataAvailable() == false) { return } let h

我已将HealthKit框架集成到我的应用程序中。HealthKit仅从应用程序启动一次。以下代码位于为HealthKit创建的singleton类中

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应用程序将启动


有谁能帮我们找出上面代码的错误吗。提前感谢。

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

完成处理程序更改为:

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

你应该看到区别。

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

完成处理程序更改为:

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

你应该看到区别。

那么,如果我想再次启动应用程序,我该怎么做呢proceed@suji你没有做错什么。始终调用授权代码,但不要期望它向用户显示授权对话框。因此,如果假设我想再次启动应用程序,我应该如何操作proceed@suji你没有做错什么。始终调用授权代码,但不要期望它向用户显示授权对话框。