2017-05-28 3 views
0

私は、iOSのHealthKitから毎日の手順を引き出す基本的なスクリプトを作成しようとしていますが、ページが起動されても何も起こりません。 HealthKitのデータを読み取る権限を要求する必要があり、どこに間違っているのか分からない。Swift - HealthKitは認証されません

は、ここに私のコードです:

import Foundation 
import UIKit 
import HealthKit 
class HealthKitPage: UIViewController { 
    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    class HealthKitManager { 
     let HealthStore = HKHealthStore() 

     func AuthoriseHealthKit() -> Bool { 
      var isEnabled = true 

      if HKHealthStore.isHealthDataAvailable() { 
       let StepCount = NSSet(object: HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)) 

       let DataTypesToWrite = NSSet(object: StepCount) 
       let DataTypesToRead = NSSet(object: StepCount) 

       HealthStore.requestAuthorization(toShare: nil, read: (StepCount as! Set<HKObjectType>)) { 
        (success, error) -> Void in 
        isEnabled = success 
       } 
      }else{ 
       isEnabled = false 
      } 

      return isEnabled 
     } 
    } 

誰もが何か提案を提供することはできますか?

答えて

0

私はHimaliの答えを使用してこの問題を解決することができたが、それはスウィフト3への変換に必要な、ここに私の作業ファイルです:

import Foundation 
import UIKit 
import HealthKit 


import UIKit 
import HealthKit 
class HealthKitPage : UIViewController 
{ 
     let healthStore: HKHealthStore = HKHealthStore() 

     override func viewDidLoad() 
     { 
       var shareTypes = Set<HKSampleType>() 

       shareTypes.insert(HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!) 



       var readTypes = Set<HKObjectType>() 
       readTypes.insert(HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!) 


       healthStore.requestAuthorization(toShare: shareTypes, read: readTypes) { (success, error) -> Void in 
          if success { 
            print("success") 
          } else { 
            print("failure") 
          } 

          if let error = error { print(error) } 


       } 





     } 
は、
0
import UIKit 
import HealthKit 
class HealthKitPage : UIViewController 
{ 
let healthStore: HKHealthStore = HKHealthStore() 

override func viewDidLoad() 
{ 
    var shareTypes = Set<HKSampleType>() 

shareTypes.insert(HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!) 



     var readTypes = Set<HKObjectType>() 
     readTypes.insert(HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!) 


     healthStore.requestAuthorizationToShareTypes(shareTypes, readTypes: readTypes) { (success, error) -> Void in 
      if success { 
       print("success") 
      } else { 
       print("failure") 
      } 

      if let error = error { print(error) } 
     } 


} 

} 
関連する問題