[关闭]
@iwanglong 2018-11-05T09:09:01.000000Z 字数 3578 阅读 462

Native - RN 互跳

技术


1、在我们刚学习rn的时候,官方提供给我们一个简单的demo,实现了native跳转RN页面

  1. NSURL *jsCodeLocation;
  2. jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
  3. RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
  4. moduleName:@"SecondProject"
  5. initialProperties:nil
  6. launchOptions:nil];
  7. UIViewController *reactNativeVC = [UIViewController new];
  8. reactNativeVC.view = rootView;
  9. // reactNativeVC.title = @"cnode";
  10. [self.navigationController setNavigationBarHidden:YES animated:YES];
  11. [self.navigationController pushViewController:reactNativeVC animated:YES];

就这么一段代码就ok了。

2、那么RN页面怎么跳回native页面呢,下边咱们代码实现以下

因为以前是搞iOS的,所以对iOS代码比较熟悉一些,就先拿OC代码来实现以下

  1. #import <Foundation/Foundation.h>
  2. #import <React/RCTBridgeModule.h>
  3. @interface OpenNativeModule : NSObject<RCTBridgeModule>
  4. @end
  5. #import "OpenNativeModule.h"
  6. #import "AppDelegate.h"
  7. #import "NativeViewController.h"
  8. @implementation OpenNativeModule
  9. RCT_EXPORT_MODULE();
  10. RCT_EXPORT_METHOD(openNativeVC:(NSString *)tag) {
  11. switch ([tag integerValue]) {
  12. case 1:
  13. dispatch_async(dispatch_get_main_queue(), ^{
  14. AppDelegate *delegate = (AppDelegate *)([UIApplication sharedApplication].delegate);
  15. UINavigationController *rootNav = delegate.navC;
  16. NativeViewController *nativeVC = [[NativeViewController alloc] init];
  17. [rootNav pushViewController:nativeVC animated:YES];
  18. });
  19. break;
  20. case 2:
  21. dispatch_async(dispatch_get_main_queue(), ^{
  22. AppDelegate *delegate = (AppDelegate *)([UIApplication sharedApplication].delegate);
  23. UINavigationController *rootNav = delegate.navC;
  24. NativeViewController *nativeVC = [[NativeViewController alloc] init];
  25. [rootNav pushViewController:nativeVC animated:YES];
  26. });
  27. break;
  28. default:
  29. break;
  30. }
  31. }
  32. @end
  1. export default class NavigatorIOSExample extends Component {
  2. _jumpToNative=()=>{
  3. nativeModule.openNativeVC('1')
  4. }
  5. render() {
  6. return (
  7. // <NavigatorIOS
  8. // initialRoute={{
  9. // component: MyScene,
  10. // title: 'My Initial Scene',
  11. // passProps: { title: 'test' },
  12. // }}
  13. // style={{ flex: 1 }} />
  14. <TouchableHighlight onPress={this._jumpToNative}>
  15. <Text style={{marginTop:64}}>这里是跳转native的</Text>
  16. </TouchableHighlight>
  17. )
  18. }
  19. }

RN项目创建的时候Application类就有了,不需要再创建。如果是集成到现有原生项目,就需要手动修改Application类。

  1. // Module类核心代码:
  2. public class OpenNativeModule extends ReactContextBaseJavaModule {
  3. private ReactContext mReactContext;
  4. public OpenNativeModule(ReactApplicationContext context) {
  5. super(context);
  6. this.mReactContext = context;
  7. }
  8. @Override
  9. public String getName() {
  10. return "OpenNativeModule";
  11. }
  12. @ReactMethod
  13. public void openNativeVC() {
  14. Intent intent = new Intent();
  15. intent.setClass(mReactContext, SettingsActivity.class);
  16. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  17. mReactContext.startActivity(intent);
  18. }
  19. }
  20. // Package类核心代码:
  21. public class TestReactPackage implements ReactPackage {
  22. @Override
  23. public List<NativeModule> createNativeModules(
  24. ReactApplicationContext reactContext) {
  25. List<NativeModule> modules = new ArrayList<>();
  26. modules.add(new OpenNativeModule(reactContext));
  27. return modules;
  28. }
  29. @Override
  30. public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
  31. return Collections.emptyList();
  32. }
  33. }
  34. // Application类核心代码:
  35. @Override
  36. protected List<ReactPackage> getPackages() {
  37. return Arrays.<ReactPackage>asList(
  38. new MainReactPackage(),
  39. new VectorIconsPackage(),
  40. new TestReactPackage()
  41. );
  42. }

更多详细还可以参考RN与原生交互(一)——基本页面跳转、简书RN笔记-RN跳转原生及数据传递

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注