ios - WatchOS 4 未接收心率样本?

标签 ios swift apple-watch healthkit watchos-4

使用来自 SpeedySloth 的 Apple 示例代码,我在模拟器上获取了心率样本,但在运行 WatchOS 4.1 的 Series 0 Apple Watch 上未获取任何样本。这是一个错误吗?还有谁有相同的问题吗?

代码:

 func startHeartRateQuery(from startDate: Date, updateHandler: @escaping ([HKQuantitySample]) -> Void) {
        let typeIdentifier = HKQuantityTypeIdentifier.heartRate
        startQuery(ofType: typeIdentifier, from: startDate) { _, samples, _, _, error in
            guard let quantitySamples = samples as? [HKQuantitySample] else {
                print("Heart rate query failed with error: \(String(describing: error))")
                return
            }
            print("heartRate samples = \(quantitySamples)")
            updateHandler(quantitySamples)

        }
    }



    //Generic helper function 
    private func startQuery(ofType type: HKQuantityTypeIdentifier, from startDate: Date, handler: @escaping
        (HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Void) {
        let datePredicate = HKQuery.predicateForSamples(withStart: startDate, end: nil, options: .strictStartDate)
        let devicePredicate = HKQuery.predicateForObjects(from: [HKDevice.local()])
        let queryPredicate = NSCompoundPredicate(andPredicateWithSubpredicates:[datePredicate, devicePredicate])

        let quantityType = HKObjectType.quantityType(forIdentifier: type)!

        let query = HKAnchoredObjectQuery(type: quantityType, predicate: queryPredicate, anchor: nil,
                                          limit: HKObjectQueryNoLimit, resultsHandler: handler)
        query.updateHandler = handler
        healthStore.execute(query)

        activeDataQueries.append(query)
    }

最佳答案

我在相同的 Apple 示例代码上遇到了完全相同的问题。几个月前在 WatchOS 4.1 上尝试过,我可以放心地假设它从 4.2 开始就坏了。

但是,我只需向健康商店请求必要的香港授权即可使其发挥作用。只需像这样重写初始化程序或 HealthStoreManager.swift 即可:

// HealthStoreManager.swift

override init() {
    super.init()
    let allTypes = Set([HKObjectType.workoutType(),
                        HKSeriesType.workoutRoute(),
                        HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
                        HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)!])

    healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in
        if !success {
            print(error?.localizedDescription ?? "")
        }
    }
}

从头开始重新安装应用程序,它现在应该可以正常工作。

关于ios - WatchOS 4 未接收心率样本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47099944/

相关文章:

ios - 为什么我的 View 总是顺时针而不是逆时针旋转?

ios - sendMessage 在模拟器中双向失败 Apple Watch <-> iPhone

ios - SpriteKit 在 [self removeAllActions] 上崩溃;

ios - MPMoviePlayerController 在加载视频之前不显示控件

swift - 带有 resultSelector 的 RxSwift withLatestFrom 无法编译

ios - Swift - 当没有数据时,UISearchBar 没有结果消息显示

swift - apple watch app 是否有可能知道它是使用 Complication 接口(interface)启动的?

ios - 在 Apple Watch App 中通过 OAuth 2.0 实现 Slack 授权

objective-c - 我打算如何释放从类方法返回的数组?

ios - cgColor 和 UIColor 的区别