2016-05-12 5 views
0

自分のアプリケーションにHealthKitフレームワークを統合しました。 HealthKitは、アプリケーションから一度だけ起動します。以下のコードは、HealthKit用に作成されたシングルトンクラスです。私はhealthkitアプリを閉じたらSwift 2.0でHealthKitが1回だけ起動されています

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() 

    } 

、のViewControllerからのボタンアクションを呼びかけています。もう一度私がシミュレータからアプリケーションを削除した場合&ボタンをクリックしてhealthkitアプリケーションが起動します。

誰でも上記のコードで何が間違っているのか教えてください。前もって感謝します。

答えて

2

許可が既に付与されている場合、アプリはそれを再度表示しません。成功ハンドラを直接呼び出します。

にあなたの completionハンドラを変更

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

、あなたは違いが表示されるはずです。

+0

だから私は再びアプリを起動したいと思ったらどうすればいいのですか – suji

+1

@suji何も間違っているわけではありません。常に認可コードを呼び出しますが、ユーザーに認可ダイアログを表示するとは限りません。 – Sulthan

+0

あなたの答えをありがとう、ありがとう。 – suji

関連する問題