如何解决调用本机模块会引发“无法识别的 objc 方法”错误
我正在通过 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.
}
}
}
但查询部分不起作用,并引发以下错误。
健康包.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”前缀搞糊涂了?
- 尝试使用不同的名称
- 您确定要传递正确的参数吗?
- 尽量让编译器更明确地指定方法签名在 objc 上应该是什么样子:
@objc(getHeartRate:rejecter:)
func getHeartRate(_ resolve: @escaping RCTPromiseResolveBlock,rejecter reject: @escaping RCTPromiseRejectBlock)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。