运行项目有两种方法

1. 到根目录,执行 react-native run-ios 命令

会开启一个本地服务,加载服务中的jsbundle文件,然后是去index.js文件

import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json'; AppRegistry.registerComponent(appName, () => App); //展示App 组件

然后就是进入注册的组件,任何组件中必定有一个render() 方法 , 该方法返回放到主屏幕上的视图

2. 到根目录,执行npm start
打开iOS工程,运行

加载JS, 要得到一个jsx的View,一定要如下代码

  #if DEBUG
NSURL *jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
NSURL *jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif self.view = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"NNHybrid2"
initialProperties:nil
launchOptions:nil];
jsCodeLocation 是一个NSURL

  Debug : 代表dev下加载一个URL
Release: rel环境时从磁盘读取
还有一个可以用http加载,如:NSURL *jsCodeLocation = [NSURL URLWithString:@"http://127.0.0.1:8081/index.bundle?platform=ios"];

moduleName
是jsx 中注册的那个appName:(注意一定要一致)
import {AppRegistry} from 'react-native';
import App from './NNHybridRN/App';
import {name as appName} from './app.json'; AppRegistry.registerComponent(appName, () => App);

Release环境时:

不会开本地服务,需要将js和资源文件打包成xxx.bundle文件,打包命令如下:

react-native bundle --entry-file index.js --bundle-output ./ios/bundle/index2.jsbundle --platform ios --assets-dest ./ios/bundle --dev false

然后把打包好的文件拖入Xcode项目。

如果有多个bundle文件也是可以加载的,修改一下 jsCodeLocation 就可以了。

下面的代码是在ViewController中加载一个RN界面:

//
// RNPageModule1VC.m
// RNTEST
//
// Created by udc on 2020/3/2.
// Copyright © 2020 udc. All rights reserved.
// #import "RNPageModule1VC.h"
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <React/RCTEventEmitter.h>
#import "PhotosPickHelper.h"
@interface RNPageModule1VC () @end @implementation RNPageModule1VC -(void)close{
[self dismissViewControllerAnimated:YES completion:nil];
} - (void)viewDidLoad {
[super viewDidLoad]; [PhotosPickHelper shareInstance].homeVC = self; // Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
closeBtn.frame = CGRectMake(self.view.frame.size.width-, , , );
closeBtn.backgroundColor = [UIColor orangeColor];
[self.view addSubview:closeBtn];
[closeBtn setTitle:@"关闭" forState:UIControlStateNormal];
[closeBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside]; [self initRCTRootView:self.moduleName];
} #define RNBounds CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.height-80) -(void)initRCTRootView:(NSString*)moduleName{ NSURL *jsCodeLocation; #if DEBUG
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
if ([self.moduleName isEqualToString:@"TestModule1"]) {
//jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"jsbundle"]; NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *docPath = [documentDirectory stringByAppendingPathComponent:@"Resource/bundle"];
jsCodeLocation = [NSURL URLWithString:[docPath stringByAppendingPathComponent:@"index.jsbundle"]]; }
else if ([self.moduleName isEqualToString:@"TestModule2"]){
//jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"index2" withExtension:@"jsbundle"]; NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *docPath = [documentDirectory stringByAppendingPathComponent:@"Resource/bundle"];
jsCodeLocation = [NSURL URLWithString:[docPath stringByAppendingPathComponent:@"index2.jsbundle"]];
} #endif RCTRootView *rnView =
[[RCTRootView alloc] initWithBundleURL: jsCodeLocation
moduleName: moduleName
initialProperties:nil
launchOptions: nil]; rnView.frame = RNBounds;
//rnView.center = self.view.center;
[self.view addSubview:rnView]; // 设置ReactInteraction的桥接文件,不设置iOS将不能调起来RN的事件(重要)!!!!!!!
//[[ReactInteraction shareInstance] setValue:rnView.bridge forKey:@"bridge"];
}
/*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end

React Native 之项目的启动的更多相关文章

  1. React Native开源项目案例

    (六).React Native开源项目: 1.Pober Wong_17童鞋为gank.io做的纯React Native项目,开源地址:https://github.com/Bob1993/Rea ...

  2. React Native 开源项目汇总

    最近闲来无事,学习了React Native开发Android APP,自我感觉RN APP的效果和Native APP比还是蛮不错,以下是找到的一些优秀源码,仅供学习参考... React Nati ...

  3. React Native(ios)项目中logo,启动屏设置

    由于logo和启动屏尺寸多,react native(ios)中没有命令可以自动生成各种的尺寸,所以可以使用以下办法:在ionic项目中生成(使用命令:ionic resources)后,再粘贴到re ...

  4. React Native新项目启动报错'React/RCTBridgeDelegate.h' file not found

    React Native版本:0.60.4 解决方法: cd ios pod deintegrate pod install 然后重新启动就好了(示例页面变样了( ⊙ o ⊙ )) END------ ...

  5. React Native 之 项目实战(一)

    前言 本文有配套视频,可以酌情观看. 文中内容因各人理解不同,可能会有所偏差,欢迎朋友们联系我. 文中所有内容仅供学习交流之用,不可用于商业用途,如因此引起的相关法律法规责任,与我无关. 如文中内容对 ...

  6. React Native开源项目如何运行(转载)

    学习任何技术,最快捷的方法就是学习完基础语法,然后模仿开源项目进行学习,React Native也不例外.React Native推出了1年多了, 开源项目太多了,我们以其中一个举例子.给大家演示下如 ...

  7. React Native初始化项目后执行react-native run-ios,构建失败

    今天是肿么了......一上班创建React Native项目,react-native run-ios运行就报错,运行不了...呜呜...... 一开始以为自己react-native run-io ...

  8. React Native开源项目如何运行(附一波开源项目)

    学习任何技术,最快捷的方法就是学习完基础语法,然后模仿开源项目进行学习,React Native也不例外.React Native推出了1年多了, 开源项目太多了,我们以其中一个举例子.给大家演示下如 ...

  9. 关于React Native init 项目时候速度太慢的解决方法

    因为init项目的时候需要下载资源,但又因为react native的网站被墙所以下载很慢,解决方法就是换成淘宝的NPM镜像 我是直接使用了命令去替换了NPM $ npm install -g cnp ...

随机推荐

  1. is_enabled()检查元素是否可以编辑 如文本框

    演示代码from selenium import webdriverdriver = webdriver.Firefox()driver.get("https://www.baidu.com ...

  2. R-CNN, Fast R-CNN, Faster R-CNN, Mask R-CNN

    最近在看 Mask R-CNN, 这个分割算法是基于 Faster R-CNN 的,决定看一下这个 R-CNN 系列论文,好好理一下 R-CNN 2014 1. 论文 Rich feature hie ...

  3. 【Python】利用豆瓣短评数据生成词云

    在之前的文章中,我们获得了豆瓣爬取的短评内容,汇总到了一个文件中,但是,没有被利用起来的数据是没有意义的. 前文提到,有一篇微信推文的关于词云制作的一个实践记录,准备照此试验一下. 思路分析 读文件 ...

  4. Canvas入门06-线段与像素边界

    我们知道,使用以下2个API可以绘制一条线段: moveTo(x, y) 向当前路径中增加一条子路径,该子路径只包含一个点,此为线段的起始点 lineTo(x, y) 将线段的下一个点加入子路径中 c ...

  5. [Git] 019 merge 命令的补充

    回顾:[Git] 017 加一条分支,享双倍快乐 的 "2.3" 1. "Fast-forward" "Git" 在合并分支时会尽可能地使用 ...

  6. [19/09/08-星期日] Python的几个概念和语法

    一.表达式.语句.程序.函数 1.表达式 就是一个类似于数学公式的东西 ,比如:10 + 5 8 - 4:表达式一般仅仅用了计算一些结果,不会对程序产生实质性的影响 如果在交互模式中输入一个表达式,解 ...

  7. java中重写

    1.重写[针对父类与子类而言]---------即java的多态性[子类与父类间有相同的名称和参数,此方法就被重写Overriding:又称:方法覆盖] 子类对父类的允许访问的方法的实现过程进行重新编 ...

  8. SVN的各种符号含义,svn的星号,感叹号,问号等含义

    黄色感叹号(有冲突):--这是有冲突了,冲突就是说你对某个文件进行了修改,别人也对这个文件进行了修改,别人抢在你提交之前先提交了,这时你再提交就会被提示发生冲突,而不允许你提交,防止你的提交覆盖了别人 ...

  9. Zend_Cache的使用

    一.Zend_Cache快速浏览 Zend_Cache 提供了一个缓存任何数据的一般方法. 在Zend Framework中缓存由前端操作,同时通过后端适配器(File, Sqlite, Memcac ...

  10. vue 还原Data里面的数据

    this.$data包含现有的data数据, this.$options.data()中是原有的data数据 还原代码 Object.assign(this.$data.searchForm, thi ...