运行项目有两种方法

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. DOM4J解析文件

    转发一篇好文 DOM4J解析文件 Dom4j和Xpath

  2. 基于 Timer是一种定时器工具

    没有依赖 通过Timer中的schedule方法启动定时任务 一般不采用此方法 /** * ------------------------------------------------------ ...

  3. Android View的Adapter

    1 Adapter适配的对象是View Adapter通过为View提供指定格式的数据来适配View,让View可以以事先约定好的方式将内容展示给用户. 所以,进行UI设计的关键是搞清楚各个View组 ...

  4. [19/10/16-星期三] Python中的模块和包、异常、操作文件

    一.模块 # 模块(module) # 模块化,模块化指将一个完整的程序分解为一个一个小的模块 # 通过将模块组合,来搭建出一个完整的程序 # 不采用模块化,统一将所有的代码编写到一个文件中 # 采用 ...

  5. 宏定义 #define CH_SENS(a,b) 0x##a##b

    #define Bin(n) LongToBin(0x##n##L) " ## ",属于宏定义中的字符(串)连接符,即,将符号两端的字符(串)接为一个整体,如以上代码中,在调用Bi ...

  6. 源码看React 事件机制

    对React熟悉的同学都知道,React中的事件机制并不是原生的那一套,事件没有绑定在原生DOM上,发出的事件也是对原生事件的包装.那么这一切是怎么实现的呢? 事件注册 首先还是看我们熟悉的代码 &l ...

  7. ambari 2.5.0源码编译安装

    参考:https://www.ibm.com/developerworks/cn/opensource/os-cn-bigdata-ambari/index.html Ambari 是什么 Ambar ...

  8. 自己写的SqlHelper,提示在调用"Fill"前,SelectCommand 属性尚未初始化.错误

    namespace 操作数据{    class SqlHelper    {        public DataSet SqlTODs(string cmdstring)        {     ...

  9. Vue框架前言

    Vue框架 Vue 框架: 官网 vue框架:渐进式JavaScript框架 vue一个环境:可以只控制页面中一个标签.可以控制一组标签.可以控制整个页面.可以控制整个项目 vue可以根据实际需求,选 ...

  10. dom的节点操作

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...