调用本机模块会引发“无法识别的 objc 方法”错误
Posted
技术标签:
【中文标题】调用本机模块会引发“无法识别的 objc 方法”错误【英文标题】:invoking native module throws "not recognised objc method" error 【发布时间】: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]
..................
上面的代码有什么问题???
【问题讨论】:
【参考方案1】:会不会被方法中的“get”前缀搞糊涂了?
-
尝试使用其他名称
您确定您传递了正确的参数吗?
尽量让编译器更明确地指定方法签名在 objc 上的外观:
@objc(getHeartRate:rejecter:)
func getHeartRate(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock)
【讨论】:
以上是关于调用本机模块会引发“无法识别的 objc 方法”错误的主要内容,如果未能解决你的问题,请参考以下文章
如何编写重用通用 JavaScript 代码的反应原生“本机模块”(桥)?