在項(xiàng)目中遇到地圖,撥打電話,清除緩存等iOS與Andiorid機(jī)制不同的功能,就需要調(diào)用原生的界面或模塊,這里說(shuō)下React Native調(diào)用iOS原生模塊,Andiorid也是大同小異
1.創(chuàng)建原生模塊私沮,實(shí)現(xiàn)“RCTBridgeModule”協(xié)議
#import <Foundation/Foundation.h>
#import "RCTBridgeModule.h"
@interface KSDMapManager : NSObject <RCTBridgeModule>
@end
2 導(dǎo)出模塊琉闪,導(dǎo)出方法
@implementation KSDMapManager
//導(dǎo)出模塊
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(gotoIM:(RCTResponseSenderBlock)callback)
{
__weak typeof(self) weakSelf = self;
self.callback = callback;
UIViewController *controller = (UIViewController*)[[[UIApplication sharedApplication] keyWindow] rootViewController];
KSDMapLocationViewController *mapVc = [[KSDMapLocationViewController alloc] init];
mapVc.handle = ^(NSString *address) {
weakSelf.itemValue = address;
NSArray *events = [[NSArray alloc] initWithObjects:self.itemValue, nil];
callback(events);
};
[controller presentViewController:mapVc animated:YES completion:nil];
}
3 js文件中調(diào)用
//創(chuàng)建原生模塊實(shí)例
var KSDMapManager = NativeModules.KSDMapManager;
//方法調(diào)用
KSDMapManager.gotoIM(
(events)=>{
this._inputReceiveAddress(events);
console.log(events);
})