如何在iOS上运行React Native应用

by Soujanya PS

通过Soujanya PS

如何在iOS上运行React Native应用 (How to run a React Native app on iOS)

I recently started to develop a React-Native app on iOS. This was my first foray into native app development. I was surprised by the ease and level of abstraction provided by React-Native’s command line interface. I was also curious to understand what happens under the hood when React-Native runs an app on a device or a simulator.

我最近开始在iOS上开发React-Native应用程序。 这是我对本机应用程序开发的首次尝试。 我对React-Native的命令行界面提供的简易性和级别感到惊讶。 我也很想知道当React-Native在设备或模拟器上运行应用程序时,幕后的情况。

I spent a lot of time going through the relevant code. There was no one place which summarized what React-Native does to get the app working. Partly, that was the motivation to come up with this piece. I want to help any other person who is starting off afresh with React-Native app development.

我花了很多时间来阅读相关代码。 没有一个地方可以概括React-Native为使应用程序正常运行所做的工作。 在某种程度上,这是提出这一部分的动机。 我想帮助任何其他刚开始使用React-Native应用程序开发的人。

React-Native provides command line utilities to run an app on iOS and Andriod simulators/devices. Without further ado, let's try to understand the what and how of the process to run React-Native apps on iOS.

React-Native提供了命令行实用程序,可在iOS和Andriod模拟器/设备上运行应用程序。 事不宜迟,让我们尝试了解在iOS上运行React-Native应用程序的过程和过程。

幕后花絮 (Behind the scenes)

React-native provides this neat utility called init. It creates a native app template for you. This template creates the relevant Xcode project files under the iOS folder of the app.

React-native提供了称为init简洁实用程序。 它为您创建一个本机应用程序模板。 该模板在应用程序的iOS文件夹下创建相关的Xcode项目文件。

React-Native apps can be launched on iOS simulators/physical devices by running the following command in the root folder of an app:

通过在应用程序的根文件夹中运行以下命令,可以在iOS模拟器/物理设备上启动React-Native应用程序:

react-native run-ios

Successful execution would open the app on a simulator or a connected device. For this to happen, there are a bunch of steps which are executed when we run the above command.

成功执行将在模拟器或连接的设备上打开该应用程序。 为此,当我们运行上述命令时,将执行许多步骤。

run-ios命令 (run-ios command)

React-Native provides a number of command line utilities to work with the app. These can be found under the local-cli folder of the React-Native node module. run-ios is one such utility which invokes the runIOS() function defined in the runIOS.js file. run-ios accepts certain options such as:

React-Native提供了许多与该应用程序一起使用的命令行实用程序。 这些可以在React-Native节点模块的local-cli文件夹下找到。 run-ios就是这样一种实用程序,它调用runIOS.js文件中定义的runIOS()函数。 run-ios接受某些选项,例如:

#Launch the app on a specific simulatorreact-native run-ios --simulator "iPhone 5"
#Pass a non-standard location of iOS directoryreact-native run-ios --project-path "./app/ios"
#Run on a connected device, e.g. Max's iPhonereact-native run-ios --device "Max\'s iPhone"
#Build the app in Release modereact-native run-ios --configuration Release

设备/仿真器选择 (Device/Simulator selection)

When no device is specified, run-ios would launch the app in Debug mode on a simulator by default. This is done by executing a series of xcrun simctl commands. They would first check the list of available simulators on Mac, pick one among them, and then boot the selected simulator.

如果未指定任何设备,则默认情况下, run-ios将在模拟器上以调试模式启动该应用程序。 这是通过执行一系列xcrun simctl命令来完成的。 他们将首先检查Mac上可用模拟器的列表,从中选择一个,然后启动选定的模拟器。

Alternatively, if you wish to run the app on a physical device, plug the device to the Mac and then pass on the device details to the run-ios command.

或者,如果您希望在物理设备上运行该应用程序,则将该设备插入Mac,然后将设备详细信息传递给run-ios命令。

The next step is to build the Xcode project of the app.

下一步是构建应用程序的Xcode项目。

建立应用程式程式码 (Building App code)

Usually, the React-Native app Xcode project can be found in the iOS folder present under the root folder. The Xcode project is built using the xcodebuild command. Any options specified to run-ios such as the configuration etc. are passed on to this command.

通常,React-Native应用程序Xcode项目可以在根文件夹下的iOS文件夹中找到。 Xcode项目是使用xcodebuild命令构建的。 为run-ios指定的所有选项(例如配置等)都将传递到此命令。

By default, the Xcode project is built in Debug scheme. Once the project is successfully built, the app is installed and launched on the simulator or the connected device.

默认情况下,Xcode项目是在Debug方案中构建的。 成功构建项目后,将在模拟器或连接的设备上安装并启动该应用程序。

在调试模式下捆绑应用程序代码 (App code bundling in Debug Mode)

During the development process, React Native loads our JavaScript code dynamically at runtime. For this, we need a server to bundle our app code and provide it as needed.

在开发过程中,React Native在运行时动态加载我们JavaScript代码。 为此,我们需要一台服务器来捆绑我们的应用程序代码并根据需要提供它。

While the Xcode project is being built in Debug mode, an instance of Metro server is also started in parallel. Metro is the bundler used by apps created by the React-Native command line interface (CLI). It is used to bundle our app code in development. This helps us with faster and easier debugging by enabling hot reloading etc.

当在Debug模式下构建Xcode项目时,还将并行启动Metro服务器的实例。 Metro是由React-Native命令行界面(CLI)创建的应用程序使用的捆绑程序。 它用于在开发中捆绑我们的应用程序代码。 通过启用热重载等功能,这有助于我们更快,更轻松地进行调试。

Metro server is configured to start on port 8081 by default. Once the app is launched in the simulator, a request is sent to the server for the bundle.

默认情况下,将Metro服务器配置为在端口8081上启动。 在模拟器中启动应用后,会将请求发送到服务器以获取捆绑软件。

#Code in AppDelegate.m sends the request for the bundle: #index.bundle to serverjsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation  moduleName:@"MobileApp               initialProperties:nil     launchOptions:launchOptions];

The server then downloads all the required dependencies, bundles the JavaScript app code and sends it back to the app. After this step, you can see the app working on the simulator or a connected device.

然后,服务器下载所有必需的依赖项,捆绑JavaScript应用程序代码,然后将其发送回应用程序。 完成此步骤后,您可以看到该应用程序正在模拟器或连接的设备上运行。

发布模式中的应用程序代码捆绑-预打包JavaScript捆绑 (App code bundling in Release Mode — Pre-packaging the JavaScript bundle)

In release mode, we have to pre-package the JavaScript bundle and distribute it inside our app. Doing this requires a code change so that it knows to load the static bundle. In AppDelegate.m file, change jsCodeLocation to point to the static bundle if you’re not in debug mode.

在发布模式下,我们必须预先打包JavaScript捆绑包并将其分发到我们的应用程序中。 这样做需要更改代码,以便知道加载静态包。 如果您未处于调试模式,则在AppDelegate.m文件中,将jsCodeLocation更改为指向静态包。

#ifdef DEBUGjsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#elsejsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif

This will now reference the main.bundle resource file. This file is created during the Bundle React Native code and images build Phase in Xcode. During this phase, react-native-xcode.sh script is run which bundles the JavaScript app code. This script can be found under the React-Native node module’s scripts folder.

现在,这将引用main.bundle资源文件。 该文件是在Bundle React Native code and images期间创建的, Bundle React Native code and images在Xcode中构建阶段。 在此阶段, react-native-xcode.sh 脚本运行 捆绑了JavaScript应用代码。 该脚本可在React-Native节点模块的scripts文件夹下找到。

从Xcode构建应用 (Building the app from Xcode)

Alternatively, the Xcode project can also be built within Xcode in Mac instead of using the React-Native CLI. Once done, the app can be launched on a simulator selected from the Xcode options or on a connected physical device.

另外,也可以在Mac的Xcode中构建Xcode项目,而不是使用React-Native CLI。 完成后,可以在从Xcode选项中选择的模拟器或连接的物理设备上启动该应用程序。

I hope this helped you understand the various steps which happen when we run a simple react-native run-ios command which magically brings up an app on iOS.

我希望这可以帮助您了解当我们运行一个简单的react-native run-ios命令神奇地在iOS上启动一个应用程序时发生的各个步骤。

Some parts of the information provided here have been sourced from the React-Native home page. The rest is a product of me snooping around the code :)

此处提供的部分信息来自React-Native 主页 。 其余的是我窥探代码的产物:)

翻译自: https://www.freecodecamp.org/news/how-to-run-a-react-native-app-on-ios-fc427be3c375/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392882.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

导出excel 后 页面按钮失效(页面假死)

在 page_load 里加上如下代码:string beforeSubmitJS "\nvar exportRequested false; \n"; beforeSubmitJS "var beforeFormSubmitFunction theForm.onsubmit;\n"; beforeSubmitJS "theForm.onsubmit function(){ \n"; …

Mysql分组查询group by语句详解

(1) group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组(2) group by可用于单个字段分组,也可用于多个字段分组 select * from employee; --------------------------------------------- | num | d_id | name | age | sex | homea…

leetcode 75. 颜色分类(双指针)

给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。 此题中,我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。 注意: 不能使用代码…

火车头如何才能设置发布的时候,如果是有html代码就直接的转换掉,互联网上笑话抽取及排重---火车头采集器的使用和MD5算法的应用...

10011311341 吕涛、10011311356李红目的:通过熟悉使用火车头采集器,在网络上采取3万条笑话并进行排重,以此来熟悉web文本挖掘的一些知识。过程:本次学习,主要分成两个部分。第一部分是笑话文本的采集,第二部…

Tcp_wrapper

在Linux进程分为:独立进程和非独立进程非独立进程:是依赖于超级守护进程的进程, 且受Xinetd 管理,并在启动服务时 必须启动例子:#chkconfig –level 2345 telnetd on关与chkconfig 的命令:#chkconfig –lis…

angular 动画_如何在Angular 6中使用动画

angular 动画介绍 (Introduction) Animation is defined as the transition from an initial state to a final state. It is an integral part of any modern web application. Animation not only helps us create a great UI but it also makes the application interesting…

win10上面安装win7的虚拟机怎么相互ping通

最近干了一些很蛋疼的事,这些都是自己踩过的坑,记录下来方便自己以后查阅 首先我的目的就是为了在自己的PC机上面部署一个SVN服务器,然后安装一个客户端,自己写的软件就可以定期入库,做好自己的版本控制,但…

新东方面试知识点记录

3.spring mvc 怎么接受http post 方式提交过来的xml数据?servlet中怎么接受? RequestMapping(value"/jsonPrase", headers {"content-typeapplication/json","content-typeapplication/xml"}) ResponseBody …

win10用计算机名访问文件夹,win10系统提示你当前无权访问该文件夹的解决方法【图文教程】...

Win10系统下,我们在访问或更改某些系统文件夹时,有时会遇到系统提示“你当前无权访问该文件夹”的情况。那么,遇到这种情况的话,我们该怎么办呢?接下来,小编就向大家分享win10系统提示“你当前无权访问该文…

.Net Micro Framework研究—实现SideShow窗体界面

基于MF系统的Windows SideShow界面是非常炫的(如下图)。既然微软能用.Net Micro Framework实现这么棒的界面效果,我想我们也能做到。 (SideShow模拟器界面和游戏程序中的右键菜单—注意菜单弹出后,其它的界面变暗了&am…

leetcode 344. 反转字符串

编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。 不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。 你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。…

事件捕获(capture)和冒泡事件(Bubble)

PS:这里是我从别人的博客中学习事件捕获和冒泡是的总结,如果你也感兴趣的话,建议你点击链接查看原博客的内容,他们写的都是很经典! 对“捕获”和“冒泡”这两个概念,我想我们对冒泡更熟悉一些&…

gulp编译css_如何用gulp缩小CSS

gulp编译cssby Vinicius Gularte由Vinicius Gularte 如何用gulp缩小CSS (How to minify your CSS with gulp) In this article, Im going to show a simple way to automatically minify your CSS files using gulp. ?在本文中,我将展示一种使用gulp自动缩小CSS文…

线段树(区间更改,区间查最值)模板

线段树(区间更改,区间查最值)模板 主要重在理解线段树,理解了怎么改都可以,还有以后不要直接抄模板,要写出自己想的一份代码 &代码&#xff1a; #include <cstdio> #include <bitset> #include <iostream> #include <set> #include <cmath>…

Unity3D项目开发一点经验

我们主要使用3dsmax2010进行制作&#xff0c;输出FBX的类型导入Unity3D中。默认情况下&#xff0c;3dsmax8可以和U3D软件直接融合&#xff0c;自动转换为FBX物体。 注意事项如下&#xff1a; 1.面数控制 在MAX软件中制作单一GameObject物体的面数不能超过65000个三角形&#xf…

leetcode 142. 环形链表 II(set/快慢指针)

给定一个链表&#xff0c;返回链表开始入环的第一个节点。 如果链表无环&#xff0c;则返回 null。 为了表示给定链表中的环&#xff0c;我们使用整数 pos 来表示链表尾连接到链表中的位置&#xff08;索引从 0 开始&#xff09;。 如果 pos 是 -1&#xff0c;则在该链表中没有…

html5 支持表格吗,html5 – 在HTML 5中使用表格很好吗?

简单规则 – 使用表格表格数据&#xff0c;使用其他元素进行演示(使用CSS设计布局)&#xff0c;如div&#xff0c;section&#xff0c;aside&#xff0c;nav等。这为他们所持有的内容提供了意义&#xff0c;而不是为所有内容使用表事实是&#xff0c;开发人员在90年代使用了表格…

css网格_我如何记住CSS网格属性

css网格The syntax for CSS Grid is foreign and hard to remember. But if you can’t remember CSS Grid’s syntax, you won’t be confident when you use CSS Grid.CSS Grid的语法是外来的&#xff0c;很难记住。 但是&#xff0c;如果您不记得CSS Grid的语法&#xff0c;…

2017年读书计划(一)

前言 这篇博文就暂时不记录技术了&#xff0c;记录下生活。对自己今年2017年做个读书计划安排。 最近在看一部网络剧 - 《花间提壶方大厨》&#xff0c;也许你们会感觉我很无聊&#xff0c;我也是被头条带坏了&#xff0c;每天上班一个小时的地下交通-地铁&#xff0c;就借助上…

.net10个必备工具

1.NUnit 编写单元测试的工具2.NDoc 自动生成代码文档的工具3.NAnt 编译解决方案的工具4.CodeSmith 自动生成代码的工具5.FxCop 检查你的代码是否按照规范编写的工具6.Snippet Compiler 编译少量代码的工具7.ASP.NET Version Switcher Visual Studio .NET Project Conve…