运行项目有两种方法

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. 中国MOOC_零基础学Java语言_第3周 循环_1奇偶个数

    第3周编程题 查看帮助 返回   第3周编程题 依照学术诚信条款,我保证此作业是本人独立完成的. 温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截 ...

  2. numpy添加新的维度

    原文链接:https://blog.csdn.net/xtingjie/article/details/72510834 numpy中包含的newaxis可以给原数组增加一个维度 np.newaxis ...

  3. 20191114 Spring Boot官方文档学习(4.7)

    4.7.开发Web应用程序 Spring Boot非常适合于Web应用程序开发.您可以使用嵌入式Tomcat,Jetty,Undertow或Netty创建独立的HTTP服务器.大多数Web应用程序都使 ...

  4. Boostrap4 li列表橫向

    Boostrap3 li元素橫向: <ul class="nav navbar-nav list-inline"> <li class="list-in ...

  5. 锋利的jQuery 要点归纳(一) jQuery选择器

    1 基本选择器 $(#id)    根据给定的id匹配一个元素$(.class)    根据给定的类名匹配元素$(element)    根据给定的元素名匹配元素$(*)    匹配所有元素$(sel ...

  6. TypeScript ES6-Promise 递归遍历文件夹中的文件

    貌似很多人都爱用这个作为写文章的初尝试,那来吧.遍历文件夹下的所有文件,如遍历文件夹下并操作HTML/CSS/JS/PNG/JPG步骤如下:1.传入一个路径,读取路径里面所有的文件:2.遍历读取的文件 ...

  7. px-em-pt等字体的不同

  8. Jmeter学习总结

    学习内容: 1.用户定义的变量 作用:多个地方使用同一个值,且该值在不同的环境下不同,方便脚本在不同环境下运行时修改. 2.基本的HTTP请求,请求方式:get 3.传入参数为json 4.HTTP信 ...

  9. jquery 关于load()加载页面遇见的坑(js代码使用不了)

  10. <input type="radio">单选按钮

    转自:http://www.divcss5.com/html/h490.shtml1 <form> 男性: <input type="radio" checked ...