安装依赖
必须安装的依赖有:Node、Watchman、Xcode 和 CocoaPods。
Node & Watchman
brew install node
brew install watchman
Yarn
Yarn是 Facebook 提供的替代 npm 的工具,可以加速 node 模块的下载。
npm install -g yarn
安装完 yarn 之后就可以用 yarn 代替 npm 了,例如用yarn代替npm install命令,用yarn add 某第三方库名代替npm install 某第三方库名。
CocoaPods
CocoaPods是用 Ruby 编写的包管理器(可以理解为针对 iOS 的 npm)。从 0.60 版本开始 react native 的 iOS 版本需要使用 CocoaPods 来管理依赖。你可以使用下面的命令来安装 CocoaPods。CocoaPods的版本需要以上。
sudo gem install cocoapods
创建新项目
npx react-native init AwesomeProject
cd 到AwesomeProject/ios目录下,在podfile第一行加一行
source 'https://github.com/CocoaPods/Specs.git'
执行
pod install
编译并运行
yarn ios
如果长时间卡在build app,可以通过xcode来运行,查看具体报错信息
我在第一次执行yarn ios 时,就遇到了这个问题,提示 /bin/sh -c /Users/Romeo-mac/Library/Developer/Xcode/DerivedData/OctoReactNative-hesxmcuspqjmszdwqrxzcleaivld/Build/Intermediates.noindex/Pods.build/Debug-iphonesimulator/FBReactNativeSpec.build/Script-46EB2E00014D80.sh
Command PhaseScriptExecution failed with a nonzero exit code
解决办法
找到node_modules/react-native/scripts/find-node.sh文件,注释以下代码
if [[ -s "$HOME/.nvm/nvm.sh" ]]; then
# shellcheck source=/dev/null
. "$HOME/.nvm/nvm.sh"
elif [[ -x "$(command -v brew)" && -s "$(brew --prefix nvm)/nvm.sh" ]]; then
# shellcheck source=/dev/null
. "$(brew --prefix nvm)/nvm.sh"
fi
继续执行yarn ios
CRAP! DAMN IT!! 很不幸又报错了,
2022-05-18T10:52:55,979: [] the permissions on /usr/local/var/run/watchman/Romeo-mac-state allow others to write to it. Verify that you own the contents and then fix its permissions by running `chmod 0700 '/usr/local/var/run/watchman/Romeo-mac-state'`
Watchman: watchman --no-pretty get-sockname returned with exit code=1, signal=null, stderr= 2022-05-18T10:52:55,979: [] the permissions on /usr/local/var/run/watchman/Romeo-mac-state allow others to write to it. Verify that you own the contents and then fix its permissions by running `chmod 0700 '/usr/local/var/run/watchman/Romeo-mac-state'`
/Users/Romeo-mac/Desktop/OctoReactNative/node_modules/metro-hermes-compiler/src/emhermesc.js:77
throw ex;
^
Error: watchman --no-pretty get-sockname returned with exit code=1, signal=null, stderr= 2022-05-18T10:52:55,979: [] the permissions on /usr/local/var/run/watchman/Romeo-mac-state allow others to write to it. Verify that you own the contents and then fix its permissions by running `chmod 0700 '/usr/local/var/run/watchman/Romeo-mac-state'`
at ChildProcess.<anonymous> (/Users/Romeo-mac/Desktop/OctoReactNative/node_modules/fb-watchman/index.js:198:18)
at ChildProcess.emit (events.js:400:28)
at maybeClose (internal/child_process.js:1058:16)
at Socket.<anonymous> (internal/child_process.js:443:11)
at Socket.emit (events.js:400:28)
at Pipe.<anonymous> (net.js:686:12)
Emitted 'error' event on Client instance at:
at spawnError (/Users/Romeo-mac/Desktop/OctoReactNative/node_modules/fb-watchman/index.js:170:10)
at ChildProcess.<anonymous> (/Users/Romeo-mac/Desktop/OctoReactNative/node_modules/fb-watchman/index.js:198:7)
at ChildProcess.emit (events.js:400:28)
[... lines matching original stack trace ...]
at Pipe.<anonymous> (net.js:686:12)
error Command failed with exit code 7.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
幸好错误信息中已经给出了提示,是mac电脑权限的问题,执行命令
chmod 0700 '/usr/local/var/run/watchman/Romeo-mac-state'
重新执行
yarn ios 或者 yarn start
bigo!!!成功了