运行项目有两种方法

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语言_第5周 数组

    第5周 数组 5.1 数组 5.2 数组计算 public class Main { public static void main(String[] args) { for (int i = 1; ...

  2. 【Spring】---【IOC入门案例】

    第一步:导入Jar包 第二步:创建类,在类里面创建方法 public class User { public void add(){ System.out.println("-------- ...

  3. JMeter常用的4种参数化方式-操作解析

    目录结构 一.JMeter参数化简介 1.JMeter参数化的概念 2.JMeter参数化方式之使用场景对比 二.JMeter参数化的4种主要方式-操作演练 1.User Parameters(用户参 ...

  4. pyinstaller如何将自己写的模块一并打包到exe中

    使用pyinstaller命令 pyinstaller -F main.py 打包时,若mian.py代码中存在引入自己写的模块,而打包成exe文件时,并不会自动引入自己写的模块,打包成功后,点击打开 ...

  5. Katalon Studio学习笔记(三)——chromedriver与当前chrome版本不符,如何替换

    首先下载chrome版本对应的chromedriver.exe文件,然后找到katalon如下图所示文件夹中,替换chromedriver.exe重新启动katalon即可. 最新适配chrome 7 ...

  6. 深入理解java:1. JVM虚拟机的构成

    1.JVM虚拟机的构成 什么是Java虚拟机? 作为一个Java程序员,我们每天都在写Java代码,我们写的代码都是在一个叫做Java虚拟机的东西上执行的. 但是如果要问什么是虚拟机,恐怕很多人就会模 ...

  7. 第十七周周总结 Swing

    考试系统 1.登录功能 用户和密码存在在哪里? 文件 2.考试功能 考试题目和答案存在哪? 文件 3.展示功能 GUI Graphical User Interface图形用户接口 #GUI Java ...

  8. 自定义SpringBoot启动控制台图标

    使用过SpringBoot的小伙伴众所周知,在启动的过程中,在控制台会首先打印spring的图标以及版本号(这里以IDEA为例) 如果需要更改这个打印图标的话, 需要以下步骤: 1.打开SpringB ...

  9. 背包dp相关

    0/1背包 给出n个物品,每个物品有Vi的价值和Wi的费用,我们总共有m块钱,求最多能得到多少价值的物品. N<=10^3,m<=10^3 记录方案数?记录输出方案? 输出方案: 对每个d ...

  10. Luogu P1084 [NOIP2012]疫情控制

    题目 首先我们二分一下答案. 然后我们用倍增让军队往上跳,最多先跳到根的子节点. 如果当前军队可以到达根节点,那么记录一下它的编号和它到达根节点后还可以走的时间. 并且我们记录根节点的叶子节点上到根节 ...