2016-07-12 6 views
1

私は健康キットからデータを読み取っているアプリケーションを作成しています。私はステップの数、ランニング+ウォーキングなどを読むことができるようになりました。今では、サイクリングの日時や時間を読み取ろうとしています。これは私がSwiftを使用してiOSアプリケーションのHealthKitからサイクリング時間を取得する方法

func readDistanceWalkingRunning(completion: (([AnyObject]!, NSError!) -> Void)!) { 
    let runningWalking = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning) 
    let predicate = HKQuery.predicateForSamplesWithStartDate(NSDate().dateByAddingTimeInterval(-86400.0), endDate: NSDate(), options: HKQueryOptions.None) 

    let stepsSampleQuery = HKSampleQuery(sampleType: runningWalking!, 
             predicate: predicate, 
             limit: 100, 
             sortDescriptors: nil) 
    { [weak self] (query, results, error) in 

     if let results = results as? [HKQuantitySample] { 

      for result in results { 
       print(" Distance was " + " \(result.quantity.doubleValueForUnit(HKUnit.mileUnit())) ") 
       print("Date was " + "\(result.startDate)") 

      } 
     } 
    } 

    // Don't forget to execute the Query! 
    executeQuery(stepsSampleQuery) 
} 

ウォーキング+実行のために使用していたもので、すべてがうまくある、しかし、私は以下のコードを使用してサイクリングに距離を読み取ろうとするとき、私はゼロの結果を取得しています。 appleHealthアプリでサイクリングデータが表示されていますが、なぜ結果がゼロになるのですか?助けてください

func readDistanceCycling(completion: (([AnyObject]!, NSError!) -> Void)!) { 
    let distanceCycling = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceCycling) 
    let predicate = HKQuery.predicateForSamplesWithStartDate(NSDate(), endDate: NSDate(), options: HKQueryOptions.None) 

    let query = HKSampleQuery(sampleType: distanceCycling!, predicate: predicate, limit: 100, sortDescriptors: nil, resultsHandler: { query, result, error in 


if result != nil 
{ 
    print("We have some Data") 
} 
else 
{ 
    print("Result is nil") 
} 

if let results = result as? [HKQuantitySample] { 

    for result in results { 
     print(" Quantity type " + " \(result.quantityType) ") 
     print("Date was " + "\(result.startDate)") 

    } 
} 
}) 

    executeQuery(query) 
    } 


    } 

答えて

1

下記の機能を使用してください。それはあなたに一番近いレコードを与える。要件に応じて変更することもできます。

func getHealthDataValue (HealthQuantityType : HKQuantityType , strUnitType : String , GetBackFinalhealthData: (((healthValues : [AnyObject]) -> Void)!)) 
{   
    if let heartRateType = HKQuantityType.quantityTypeForIdentifier(HealthQuantityType.identifier) 
    { 
     if (HKHealthStore.isHealthDataAvailable() ){ 

      let sortByTime = NSSortDescriptor(key:HKSampleSortIdentifierEndDate, ascending:false) 

      //   let timeFormatter = NSDateFormatter() 
      //   timeFormatter.dateFormat = "hh:mm:ss" 
      //yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ 

      let dateFormatter = NSDateFormatter() 
      dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss" 

     let query = HKSampleQuery(sampleType:heartRateType, predicate:nil, limit:7, sortDescriptors:[sortByTime], resultsHandler:{(query, results, error) in 

      guard let results = results else { 

       //include the healthkit error in log 
       if let errorDescription = error!.description as String? 
       { 

        GetBackFinalhealthData (healthValues: ["nodata"]) 
       } 
       return 
      } 

      var arrHealthValues  = [AnyObject]() 

      for quantitySample in results { 
       let quantity = (quantitySample as! HKQuantitySample).quantity 
       let healthDataUnit : HKUnit 
       if (strUnitType.length > 0){ 
        healthDataUnit = HKUnit(fromString: strUnitType) 
       }else{ 
        healthDataUnit = HKUnit.countUnit() 
       } 

       let tempActualhealthData = "\(quantity.doubleValueForUnit(healthDataUnit))" 
       let tempActualRecordedDate = "\(dateFormatter.stringFromDate(quantitySample.startDate))" 
       if (tempActualhealthData.length > 0){ 
        let dicHealth : [String:AnyObject] = [HealthValue.kIdentifierValue :tempActualhealthData , HealthValue.kRecordDate :tempActualRecordedDate , HealthValue.kIdentifierDisplayUnit : strUnitType ] 

        arrHealthValues.append(dicHealth) 
       } 
      } 

      if (arrHealthValues.count > 0) 
      { 
       GetBackFinalhealthData (healthValues: arrHealthValues) 
      } 
      else 
      { 
       GetBackFinalhealthData (healthValues: [HealthValue.kNoData]) 
      } 
     }) 
     (self.HealthStore as! HKHealthStore).executeQuery(query) 
    } 
} 

}次のように機能上

使用。 ここでは、型とユニットを渡す必要があります。

self.getHealthDataValue(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodPressureDiastolic), strUnitType: "mmHg" 
       ) { (arrHealth) -> Void in     
      } 
+0

私にサイクリング記録がありますか? :-o – Byte

+0

このメソッドは、必要なものすべてを与えます。 – iMHitesh

+0

しかし、あなたは適切にメソッドを呼び出す必要があります – iMHitesh

関連する問題