我定义了一个的ios模块,包含NativeTest.h和NativeTest.m,但在js里是undefined
来源:11-6 React Native原生模块的封装(iOS)
qq_张家景萁_0
2018-06-19
//NativeTest.h
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
@interface NativeTest : NSObject<RCTBridgeModule>
@end
//NativeTest.m
#import "NativeTest.h"
@implementation NativeTest
// 导出模块,不添加参数即默认为这个类名
RCT_EXPORT_MODULE();
// 导出方法,桥接到js的方法返回值类型必须是void
RCT_EXPORT_METHOD(doSomething:(NSString *)testStr){
NSLog(@"%@ ===> doSomething",testStr);
}
@end
//js
import { NativeModules } from 'react-native';
NativeModules.NativeTest //undefined用课件里面的的分享模块也是 undefined
写回答
1回答
-
CrazyCodeBoy
2018-06-20
写法是对的;
检查一下你的NativeTest.h与NativeTest.m有没有正确导入到项目中,用XCode打开iOS项目看这两个文件是否被添加到项目中;
关闭RN服务器,重新运行项目,或者通过XCode的运行按钮运行项目;
也可以参考:https://facebook.github.io/react-native/docs/native-modules-ios.html#ios-calendar-module-example
另外也可以通过如下代码来调试,看下NativeModules中都有哪些输出:
import { NativeModules } from 'react-native'; debugger console.log( NativeModules); module.exports = NativeModules.NativeTest;00
相似问题