美文网首页React Native(RN)开发
ReactNative-从RN端跳转到原生界面

ReactNative-从RN端跳转到原生界面

作者: 水果团团长 | 来源:发表于2017-08-07 11:21 被阅读851次

    //实现跳转
    //可以做一次封装,这里只是展示功能
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    app.nav.navigationBarHidden = NO;
    [app.nav pushViewController:vc animated:YES];
    

    可以这么做的前提就是在app初始化的时候,rootviewcontroller需要设置为一个导航控制器:
    在AppDelegate.m的

    - (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
    

    中添加或者修改:

    RCTRootView *rootView = 
    [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                moduleName:@"AwesomeProject"
                         initialProperties:nil
                             launchOptions:launchOptions];
     //已有的RN界面/模块
    rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    SomeViewController *rootViewController = [SomeViewController new];//某个视图控制器
    rootViewController.view = rootView;
    self.nav = [[UINavigationController alloc]initWithRootViewController:rootViewController];
    self.window.rootViewController = self.nav;
    self.nav.navigationBarHidden = YES;
    

    RN界面可以看做是属于NavigateController管理的rootVC的View,
    这样可以做到原生部分与RN生成的部分相互之间没有额外的干扰,相互跳转不会产生问题,当然,在这种情况下,跳转到原生部分后,开发时的那些Command快捷键就失效了,安卓的情况也是类似,只不过跳转使用的可能是另一个Activity,键盘响应不一样。
    这样在RN端 调用某个原生插件的时候,就可以实现跳转了,例如:

        [[SCApp loginManager] 
                  login:localUserId
                  token:localToken
                  completion:^(NSError *error) {
                  if(sessionInfo&&sessionInfo[@"toId"]){
                          SCSession *session = [SCSession session:sessionInfo[@"toId"] type: SCSession TypeP2P];
                           //初始化SessionViewCongtroller
                          SCSession ViewController *vc = [[SCSession ViewController alloc] initWithSession:session];
                          //实现跳转
                          AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                          app.nav.navigationBarHidden = NO;
                          [app.nav pushViewController:vc animated:YES];
                  }else{
                          UIAlertView *uav = [[UIAlertView alloc]initWithTitle:@"提示" message:@"用户会话已过期" delegate:nil cancelButtonTitle:@"忽略" otherButtonTitles:nil, nil];
                          [uav show];
                  }
    }];
    

    相关文章

      网友评论

        本文标题:ReactNative-从RN端跳转到原生界面

        本文链接:https://www.haomeiwen.com/subject/csvolxtx.html