【问题标题】:invoking native module throws "not recognised objc method" error调用本机模块会引发“无法识别的 objc 方法”错误
【发布时间】:2021-03-17 07:56:41
【问题描述】:

我正在通过 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] {
                   ..................

                  }

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

【问题讨论】:

    标签: ios swift xcode healthkit


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      • 2020-11-03
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多