You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HealthKit获取Workout路线数据失败:Authorization not determined错误求助

解决HealthKit读取Workout路线数据时的"Authorization not determined"错误

问题根源

你碰到的Error Domain=com.apple.healthkit Code=5 "Authorization not determined"错误,本质是权限范围不足:

  • 你仅申请了HKObjectType.workoutType()的读取权限,但Workout关联的路线数据属于独立的HKSeriesType.workoutRoute()类型,必须单独申请该类型的读取权限才能访问。
  • 即使能正常加载Workout列表,访问其关联的路线数据仍需额外授权。

修复步骤

  1. 更新权限申请集合:在请求HealthKit授权时,将HKSeriesType.workoutRoute()加入到读取权限列表中。
  2. 修正路线数据查询逻辑:原有代码仅发起了路线查询但未处理返回数据,需使用HKWorkoutRouteQuery提取具体的位置坐标。

修改后的代码片段

1. ContentView 授权逻辑更新

private func requestAuthorization() {
    let typesToRead: Set<HKObjectType> = [
        HKObjectType.workoutType(),
        HKSeriesType.workoutRoute() // 添加路线数据读取权限
    ]

    healthStore.requestAuthorization(toShare: nil, read: typesToRead) { success, error in
        if let error = error {
            print("Authorization request failed: \(error.localizedDescription)")
            return
        }
        
        if success {
            print("Authorization granted")
            self.isPresented = true
        } else {
            print("Authorization denied")
        }
    }
}

2. WorkoutListViewModel 路线数据读取逻辑更新

func getLocationData(for workout: HKWorkout, completion: @escaping (CLLocation?) -> Void) {
    // 查询关联的路线样本
    let routePredicate = HKQuery.predicateForObjects(from: workout)
    let routeQuery = HKSampleQuery(
        sampleType: HKSeriesType.workoutRoute(),
        predicate: routePredicate,
        limit: HKObjectQueryNoLimit,
        sortDescriptors: nil
    ) { [weak self] query, samples, error in
        guard let self = self, error == nil, let routes = samples as? [HKWorkoutRoute] else {
            print("Failed to fetch route data: \(error?.localizedDescription ?? "Unknown error")")
            completion(nil)
            return
        }
        
        guard let targetRoute = routes.first else {
            completion(nil)
            return
        }
        
        // 从路线中提取具体位置数据
        let locationQuery = HKWorkoutRouteQuery(route: targetRoute) { query, locations, isDone, error in
            if let error = error {
                print("Failed to read route locations: \(error.localizedDescription)")
                completion(nil)
                return
            }
            
            // 返回第一个位置(可根据需求改为返回所有位置数组)
            if let locations = locations, !locations.isEmpty {
                completion(locations.first)
            } else if isDone {
                completion(nil)
            }
        }
        
        self.healthStore.execute(locationQuery)
    }
    
    self.healthStore.execute(routeQuery)
}

额外说明

  • 确保Info.plist中已添加NSHealthReadUsageDescription(读取健康数据的描述文案),否则权限弹窗无法正常显示。
  • HKAnchoredObjectQuery更适合监听路线数据的实时变化,若仅读取历史路线数据,使用HKSampleQuery更简洁。

内容的提问来源于stack exchange,提问作者lacking-cypher

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 23:05:02