ios - Trying to get steps using HealthKit but it's always returning 0.0 -
i'm trying steps half hour ago , i'm using method discussed here. following code:
func getsteps(completion: @escaping (double) -> void) { let stepsquantitytype = hkquantitytype.quantitytype(foridentifier: .stepcount)! let = date() let calendar = calendar.current let halfhouragodate = calendar.date(byadding: .minute, value: -30, to: now) if let date = halfhouragodate { let predicate = hkquery.predicateforsamples(withstart: date, end: now, options: .strictstartdate) let query = hkstatisticsquery(quantitytype: stepsquantitytype, quantitysamplepredicate: predicate, options: .cumulativesum) { (_, result, error) in var resultcount = 0.0 guard let result = result else { completion(resultcount) return } if let sum = result.sumquantity() { resultcount = sum.doublevalue(for: hkunit.count()) return } dispatchqueue.main.async { completion(resultcount) } } healthstore.execute(query) } else { print("error mainvc, date being cast nil") } }
when try steps, code:
var todayssteps: double = 0 getsteps(completion: { (resultcount) -> void in todayssteps = resultcount }) print(todayssteps) // comes out 0.0
yet whenever run code, code returns 0.0. i've enabled healthkit, , authorized steps, i'm not sure reason is. note in variable declarations:
let healthstore = hkhealthstore()
Comments
Post a Comment