HealthKit 授权已授予并在模拟器上工作,但不在实际设备上
Posted
技术标签:
【中文标题】HealthKit 授权已授予并在模拟器上工作,但不在实际设备上【英文标题】:HealthKit authorization granted and working on simulator but not on actual device 【发布时间】:2017-12-30 11:08:08 【问题描述】:我正在为 Apple Watch 开发一个锻炼应用程序,但在我的实际手表上使用 HealthKit 时遇到了一些问题。
请求授权在模拟器和我的设备上都有效,并且在每次启动时都显示为成功。然而,在我的设备上查询和读取样本失败,但在模拟器上却没有。
启动后,授权成功,需要查询或保存锻炼时提示“未确定授权”。
两项权利均已将 HealthKit 设置为 YES,HealthKit 和后台功能已开启,ios Info.plist 中提供了 NSHealthShareUsageDescription 和 NSHealthUpdateUsageDescription 键。
授权码
// Configure write values
let writeTypes: Set<HKSampleType> = [.workoutType(),
HKSampleType.quantityType(forIdentifier: .heartRate)!,
HKSampleType.quantityType(forIdentifier: .activeEnergyBurned)!,
HKSampleType.quantityType(forIdentifier: .stepCount)!,
HKSampleType.quantityType(forIdentifier: .distanceCycling)!,
HKSampleType.quantityType(forIdentifier: .distanceSwimming)!,
HKSampleType.quantityType(forIdentifier: .distanceWalkingRunning)!,
HKSampleType.quantityType(forIdentifier: .swimmingStrokeCount)!]
// Configure read values
let readTypes: Set<HKObjectType> = [.activitySummaryType(), .workoutType(),
HKObjectType.quantityType(forIdentifier: .heartRate)!,
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
HKObjectType.quantityType(forIdentifier: .stepCount)!,
HKObjectType.quantityType(forIdentifier: .distanceCycling)!,
HKObjectType.quantityType(forIdentifier: .distanceSwimming)!,
HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)!,
HKObjectType.quantityType(forIdentifier: .swimmingStrokeCount)!]
// Create health store
let healthStore = HKHealthStore()
// Use it to request authorization for our types
healthStore.requestAuthorization(toShare: writeTypes, read: readTypes) (success, error) in
if success
print("Success: authorization granted")
else
print("Error: \(error?.localizedDescription ?? "")")
查询代码(Udemy课程)
func startQuery(_ quantityTypeIdentifier: HKQuantityTypeIdentifier)
// We only want data points after our workout start date
let datePredicate = HKQuery.predicateForSamples(withStart: workoutStartDate, end: nil, options: .strictStartDate)
// And from our current device
let devicePredicate = HKQuery.predicateForObjects(from: [HKDevice.local()])
// Combine them
let queryPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [datePredicate, devicePredicate])
// Write code to receive results from our query
let updateHandler: (HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Void = query, samples, deletedObjects, queryAnchor, error in
//safely typecast to a quantity sample so we can read values
guard let samples = samples as? [HKQuantitySample] else return
//process the samples
print("Start processing samples")
self.process(samples, type: quantityTypeIdentifier)
// Create the query out of our type (e.g. heart rate), predicate and result handling code
let quantityType = HKObjectType.quantityType(forIdentifier: quantityTypeIdentifier)!
let query = HKAnchoredObjectQuery(type: quantityType, predicate: queryPredicate, anchor: nil, limit: HKObjectQueryNoLimit, resultsHandler: updateHandler)
// Tell HealthKit to re-run the code every time new data is available
query.updateHandler = updateHandler
// Start the query running
healthStore.execute(query)
// Stach it away so we can stop it later
activeDataQueries.append(query)
【问题讨论】:
【参考方案1】:我将授权代码放在我的 ExtensionDelegate 中,而不是我正在查询并开始工作的同一个文件中。
奇怪的是,它在模拟器上工作,但在实际设备上却没有。
【讨论】:
以上是关于HealthKit 授权已授予并在模拟器上工作,但不在实际设备上的主要内容,如果未能解决你的问题,请参考以下文章
HealthKit 会不断更新模拟器上的样本数据,但不会更新 Apple Watch 上的实际数据
如何设置 HealthKit 请求授权 tableview 外观