查询不工作 - React Native 应用 + Xcode

query not working - React Native app + Xcode

我的 HealthKit 查询不工作。
我正在尝试计算步数,但我对我的代码的哪一部分不正确感到困惑。
当我构建应用程序时,它要么抛出“updateStepsCount 不是可识别的 objc 方法”,要么抛出“未处理的承诺拒绝”。
我哪里错了!!!

Controller.swift:

@objc
  func updateStepsCount(_ statisticsCollection: HKStatisticsCollection, _ resolve: @escaping RCTPromiseResolveBlock,
                        rejecter reject: @escaping (RCTPromiseRejectBlock) -> Void) {
      
      let stepType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
      
      let startDate = Calendar.current.date(byAdding: .day, value: -7, to: Date())
      
      let anchorDate = Date.mondayAt12AM()
      
      let daily = DateComponents(day: 1)
      
      let predicate = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: .strictStartDate)
      
      let query = HKStatisticsCollectionQuery(quantityType: stepType, quantitySamplePredicate: predicate, options: .cumulativeSum, anchorDate: anchorDate, intervalComponents: daily)

          healthStore.execute(query)
    
    struct Step {
        let id = UUID()
      var count: Int?
        var date: Date?
    }
      let endDate = Date()
      
    statisticsCollection.enumerateStatistics(from: startDate!, to: endDate) { (statistics, stop) in
          
          let count = statistics.sumQuantity()?.doubleValue(for: .count())
          let steps = Int(count ?? 0)
        
        var stepCount = [Step]()
        var tempStepCount = Step(count: steps, date: Date())
          
        tempStepCount.count = steps
        tempStepCount.date = startDate
        stepCount.append(tempStepCount)
      }
    resolve(Step())
  }


Controller.m

RCT_EXTERN_METHOD(updateStepsCount: (HKStatisticsCollection)someStatisticsCollection
                  resolve: (RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)

JS 端:

 clickHandler = async() => {
    const login = HealthkitController.updateStepsCount()
    .then(result => console.warn(result));
  }

那是因为您的 updateStepsCount 方法需要参数 statisticsCollection 作为第一个参数。当您尝试在 js 中调用 updateStepsCount() 时,它会在 objc 桥中转换为 updateStepsCount 但是您没有这种方法,您有 updateStepsCount: 并且这个方法有一个参数,因此您必须传递一个参数

NOTE: React Native bridge works only with standard JSON types so input parameters can be standard types (number, string, dictionary etc.) but you can also use RCTConvert to map they to custom ones. Result of your promise must be standard type as well so you can not return your struct. (https://reactnative.dev/docs/native-modules-ios#argument-types)