2015-03-02 13 views
5

Tôi đang cố gắng sử dụng bộ đếm bước của HealthKit và cho đến nay đây là những gì tôi có. Nó không thất bại nhưng tôi không thấy bất kỳ hoạt động nào. Tôi đang thiếu gì?HealthKit Step Counter

import UIKit 
import HealthKit 

class ViewController: UIViewController { 
    let healthStore: HKHealthStore? = { 
     if HKHealthStore.isHealthDataAvailable() { 
      return HKHealthStore() 
     } else { 
      return nil 
     } 
     }() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     let endDate = NSDate() 
     let startDate = NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitMonth, value: -1, toDate: endDate, options: nil) 

     let weightSampleType = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount) 
     let predicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: .None) 

     let query = HKSampleQuery(sampleType: weightSampleType, predicate: predicate, limit: 0, sortDescriptors: nil, resultsHandler: { 
      (query, results, error) in 
      if results == nil { 
       println("There was an error running the query: \(error)") 
      } 

      dispatch_async(dispatch_get_main_queue()) { 
       var dailyAVG:Double = 0 
       for steps in results as [HKQuantitySample] 
       { 
        // add values to dailyAVG 
        dailyAVG += steps.quantity.doubleValueForUnit(HKUnit.countUnit()) 
        println(dailyAVG) 
        println(steps) 
       } 
      } 
     }) 


    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 

Trả lời

8

Bạn vừa quên thực sự truy vấn. Đây là tất cả các bạn cần ở phần cuối của phương pháp viewDidLoad của bạn:

healthStore?.executeQuery(query)

Cheers!

Các vấn đề liên quan