Build the development environment React Native target platform ios under MacOS

Official website: https://reactnative.cn/docs/getting-started

Development platform: macOS
target platform: iOS

Install dependencies

The dependencies that must be installed are: Node, Watchman, Xcode and CocoaPods.

Although you can use any editor to develop applications (write js code), you must still install Xcode to obtain the tools and environment required to compile iOS applications

We recommended Homebrewto install Nodeand Watchman. Execute the following command in the command line to install
If you do not have it, please install it firstHomebrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install node
brew install watchman

If you have installed Node, please check if its version is above v12. After installing Node, it is recommended to set up npm mirror (Taobao source) to speed up the subsequent process (or use scientific Internet tools).

注意:不要使用 cnpm!cnpm 安装的模块路径比较奇怪,packager 不能正常识别!!!

# 使用nrm工具切换淘宝源
npx nrm use taobao

# 如果之后需要切换回官方源可使用
npx nrm use npm

Watchman is a tool provided by Facebook to monitor file system changes. Installing this tool can improve performance during development (packager can quickly capture file changes to achieve real-time refresh).

The following is the brew node watchman version of my computer
Insert picture description here

Yarn

Yarn is a tool provided by Facebook to replace npm, which can speed up the download of node module

npm install -g yarn

After installing npm yarn can be used instead of yarn, for example, instead of npm install command with yarn, with yarn add a third-party library name instead of npm install a third-party library name
yran another source configuration refer to my blog post: Mac installation yarn set Mirror

CocoaPods

CocoaPods is a package manager written in Ruby. Starting from version 0.60, React native iOS version requires CocoaPods to manage dependencies. You can use the following command to install cocoapods.

Of course, the installation may not be smooth, please try to over the wall or find some domestic mirror source available.
ps If this step is not successful, the project will fail to start

sudo gem install cocoapods

Or you can use brew to install

brew install cocoapods

View version

pod --version

In addition, it seems that the latest version cannot be installed below the ruby2.6 version, which means that if you use a macOS version lower than 10.15 (Catalina), you cannot install it directly. You can try to install an older version.
As sudo gem install cocoapods -v 1.8.4
because my computer is using a version lower than 10.15 so Isudo gem install cocoapods -v 1.8.4

Xcode

React Native currently requires Xcode 10 or higher. You can download it through the App Store or on the official website of the Apple Developer. This step will install Xcode IDE, Xcode's command line tools and iOS simulator at the same time.
ps This software is very large and needs to wait for a certain time to download

Xcode's command line tools
Start Xcode and check whether a certain version of Command Line Tools is installed in the Xcode | Preferences | Locations menu. Xcode's command-line tools include some necessary tools, such as git.
Insert picture description here

Create new project

If you have previously installed the old react-native-cli command-line tool globally, please uninstall it using npm uninstall -g react-native-cli to avoid some conflicts.

Use React Native's built-in command-line tool to create a new project called "FirstApp". This command-line tool does not need to be installed, you can directly use the npx command that comes with node (note that the init command will create the latest version by default):

这一步非常重要 鬼知道我在这里经历了什么...The CocoaPods warehouse is also difficult to access in China. If the installation step card in CocoaPods is long, you can try this domestic mirror

cd 文件路径
npx react-native init FirstApp

Insert picture description here

Run React Native application

Run in your project directory yarn iosoryarn react-native run-ios

cd AwesomeProject
yarn ios
# 或者
yarn react-native run-ios

Tip : If this command does not work properly, please use Xcode to run to see the specific error (run-ios reported no specific information). Note that the main project file after version 0.60 is .xcworkspace, not .xcodeproj!

ok i started successfully
Insert picture description here

Published 41 original articles · Likes2 · Visits 1836

Guess you like

Origin blog.csdn.net/weixin_43883485/article/details/105426882