NativeModules.MyNativeModule在iOS中未定义React Native 0.61.2

时间:2019-10-19 01:57:15

标签: ios reactjs react-native

我有一个正在编写的React Native库。到目前为止,它仍在本地,所以我用

进行安装

yarn add ../MyNativeModule

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

const MyNativeModule = NativeModules.MyNativeModule;。这在android上可以正常工作,但在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

我的podspec:

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}"

我现在已经整天都在尝试,但是什么都没做。一些帮助会很棒。

1 个答案:

答案 0 :(得分:0)

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

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

@end