NativeModules.MyNativeModule 在 iOS 中的 react native 0.61.2 中未



我有一个正在编写的 react 原生库。到目前为止,它仍保留在本地,所以我安装了它

yarn add ../MyNativeModule

然后在我的代码中,我将其导入为

const MyNativeModule = NativeModules.MyNativeModule;.这在安卓上工作正常,但在 iOS 上,它不起作用。我得到MyNativeModuleundefined.

我的原生模块:

android
...
ios
MyNativeModule.h
MyNativeModule.m
MyNativeModule.xcodeproj
index.js
MyNativeModule.podspec

MyNativeModule.h

#import <React/MyNativeModule.h>
@interface MyNativeModule : NSObject <RCTBridgeModule>
@end

MyNativeModule.m

#import "MyNativeModule.h"
@implementation MyNativeModule
RCT_EXPORT_MODULE()
@end

我的豆荚规格:

require 'json'
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
Pod::Spec.new do |s|
s.name         = "MyNativeModule"
s.version      = package['version']
s.summary      = package['description']
s.license      = package['license']
s.authors      = package['author']
s.homepage     = package['repository']['url']
s.platform     = :ios, "9.0"
s.ios.deployment_target = '9.0'
s.source       = { :git => "" }
s.source_files  = "ios/**/*.{h,m}"
s.dependency 'React'
end

我试过:

1. Removed and re-installed everything.
2. Cleared caches of pods and metro.
3. I tried changing the s.source on my podspec to the local repo.
4. I tried changing the s.source_files to "ios/*.{h,m}"

我已经尝试了一整天,但没有任何效果。一些帮助会很棒。

好吧,看起来我只需要在MyNativeModule.m中实现一个函数。因此,如果没有至少一个函数,它就无法工作。

RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location)
{
RCTLogInfo(@"Pretending to create an event %@ at %@", name, location);
}
@end

最新更新