调用本机模块会引发 "not recognised objc method" 错误

invoking native module throws "not recognised objc method" error

我正在通过 React Native 的 NativeModules 调用 Healthkit。
我可以让授权部分工作。

healthkit.m:

RCT_EXTERN_METHOD(requestAuthorization)

healthkit.swift:

        @objc
        func requestAuthorization() {
           let writeDataTypes : Set<HKSampleType> = [ HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!,
                                                      HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureDiastolic)!,
                                                      HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureSystolic)!,
                                                      HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMassIndex)!,
                                                      HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodGlucose)!,
                                                      HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyTemperature)!
                                                      ]
           
           let readDataTypes : Set = [HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!,
                                                HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!,
                                                HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureDiastolic)!,
                                                HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureSystolic)!,
                                                HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.biologicalSex)!,
                                                HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMassIndex)!,
                                                HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodGlucose)!,
                                                HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyTemperature)!,
                                                HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.bloodType)!,
                                                HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.dateOfBirth)!]

                     
                     healthStore.requestAuthorization(toShare: writeDataTypes, read: readDataTypes) { (success, error) in
                       
                         if (self.healthStore.authorizationStatus(for: HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodGlucose)!) == .sharingAuthorized) {
                             print("Permission Granted to Access bloodGlucose")
                         } else {
                             print("Permission Denied to Access bloodGlucose")
                         }
                       
                         if !success {
                             // Handle the error here.
                         }
                     }
           }

但是查询部分不起作用,它会抛出以下错误。
healthkit.m

RCT_EXTERN_METHOD(getHeartRate: (RCTPromiseResolveBlock)resolve
                                 rejecter: (RCTPromiseRejectBlock)reject)

healthkit.swift:

              @objc
              func getHeartRate(_ resolve: @escaping RCTPromiseResolveBlock,
                                rejecter reject: @escaping RCTPromiseRejectBlock) {
                let heartRate = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)
                let startOfDate = Calendar.current.startOfDay(for: startDate)
                let endOfDate = Calendar.current.endOfDay(for: endDate)
                print("dat:",startOfDate, endOfDate)
                let predicate = HKQuery.predicateForSamples(withStart: startOfDate, end: endOfDate, options: .strictStartDate)
                let query = HKSampleQuery(sampleType: heartRate!, predicate: predicate, limit: 20, sortDescriptors: nil) { (query, results, error) in
                            for result in results as! [HKQuantitySample] {
                   ..................

                  }

上面的代码有什么问题???

会不会被方法中的“get”前缀弄糊涂了?

  1. 尝试使用不同的名称
  2. 您确定传递的参数正确吗?
  3. 尝试让编译器更明确地指定方法签名在 objc 上的确切外观:
@objc(getHeartRate:rejecter:)
func getHeartRate(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock)