参考:https://segmentfault.com/a/1190000003819099
http://reactnative.cn/docs/0.28/native-modules-ios.html#content
OC文件:
————————TestManager.h——————
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "RCTBridgeModule.h"
@interface TestManager : NSObject <RCTBridgeModule>
@end
————————TestManager.m——————
#import "TestManager.h"
@implementation TestManager
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location)
{
NSLog(@"Pretending to create an event %@ at %@", name, location);
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
view.backgroundColor = [UIColor redColor];
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication].keyWindow addSubview:view];
});
}
@end
JS使用:
import {
NativeModules,
} from 'react-native';
testMethod() {
TestManager.addEvent('Test', 'Test Demo');
}
。。。。。