入门体验
课程基于2024年React Native 0.73最新版本
iOS 环境搭建和第一个项目跑起来
开发环境
使用的环境是 MacOS系统 iOS平台
CocoaPods已经安装1.11.2版本
homebrew和Node & Watchman大家自行安装,环境这块大家根据官方网站操作即可
版本号不一定要严格一致,比如我的node就是v20.8.1
创建工程
mkdir -p ~/Documents/rn/proj # 创建工程目录(可以创建到其他地方,最好不要有中文、空格等)
npx react-native@latest init AwesomeProject # 创建工程,这个过程挺慢,慢慢等吧
安装过程中看到比较慢的地方是 Downloading template,也就是在下载模板.
这地方明显可以优化,比如我们提前下载好模板放到内网环境,然后使用--template来使用下载好的模板
安装过程中的提示 Do you want to install CocoaPods now? Only needed if you run your project in Xcode directly … no
我已经安装了CocoaPods,但是这里竟然还提示我安装(安装速度还非常慢),直接键盘输入n
即可跳过安装
info 💡 To enable automatic CocoaPods installation when building for iOS you can create react-native.config.js with automaticPodsInstallation field.
For more details, see https://github.com/react-native-community/cli/blob/main/docs/projects.md#projectiosautomaticpodsinstallation
✔ Initializing Git repository
Run instructions for Android:
• Have an Android emulator running (quickest way to get started), or a device connected.
• cd "/Users/lichanghong/Documents/xxx/proj/AwesomeProject" && npx react-native run-android
Run instructions for iOS:
• cd "/Users/lichanghong/Documents/xxx/proj/AwesomeProject/ios"
• Install Cocoapods
• bundle install # you need to run this only once in your project.
• bundle exec pod install
• cd ..
• npx react-native run-ios
- or -
• Open AwesomeProject/ios/AwesomeProject.xcodeproj in Xcode or run "xed -b ios"
• Hit the Run button
Run instructions for macOS:
• See https://aka.ms/ReactNativeGuideMacOS for the latest up-to-date instructions.
根据日志信息操作一遍,bundle install
和bundle exec pod install
都是比较耗时的操作,有的是从github下载,所以可能会各种失败,重试即可(有梯子更佳)。
后续这块是可以将组件私有化来解决的,我们后面也会一步一步的教大家处理
俩小时后,终于pod install
完成,运行iOS项目
按照提示输入i
,过一会竟然报错了!报错信息:
error Failed to install the app on the device because we couldn't execute the "ios-deploy" command. Please install it by running "brew install ios-deploy" and try again.
错误处理
根据提示执行一下安装 brew install ios-deploy,安装完成后重新执行 npx react-native run-ios
info Building (using "xcodebuild -workspace AwesomeProject.xcworkspace -configuration Debug -scheme AwesomeProject -destination id=00008101-001275D93643001E")
info 💡 Tip: Make sure that you have set up your development environment correctly, by running npx react-native doctor. To read more about doctor command visit: https://github.com/react-native-community/cli/blob/main/packages/cli-doctor/README.md#doctor
error Failed to build iOS project. "xcodebuild" exited with error code '70'. To debug build logs further, consider building your app with Xcode.app, by opening 'AwesomeProject.xcworkspace'.
Command line invocation:
/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -workspace AwesomeProject.xcworkspace -configuration Debug -scheme AwesomeProject -destination id=00008101-001275D93643001E
User defaults from command line:
IDEPackageSupportUseBuiltinSCM = YES
2024-03-19 17:06:41.576 xcodebuild[7318:72965] Writing error result bundle to /var/folders/tq/stv_grp52rn8ftjm4d_c7_fw0000gn/T/ResultBundle_2024-19-03_17-06-0041.xcresult
xcodebuild: error: Unable to find a destination matching the provided destination specifier:
{ id:00008101-001275D93643001E }
Ineligible destinations for the "AwesomeProject" scheme:
{ platform:iOS, id:dvtdevice-DVTiPhonePlaceholder-iphoneos:placeholder, name:Any iOS Device, error:iOS 17.4 is not installed. To use with Xcode, first download and install the platform }
使用xcode打开iOS工程发现失败原因,因为我的macos系统刚刚升级到最新,xcode也刚升级到Version 15.3 (15E204a)
,没有安装模拟器,直接点击get按钮安装即可

安装完以后的样子
**error** Failed to build iOS project. "xcodebuild" exited with error code '65'. To debug build logs further, consider building your app with Xcode.app, by opening 'AwesomeProject.xcworkspace'.
。。。
。。。
省略一大堆信息
warning: Run script build phase '[CP-User] [Hermes] Replace Hermes for the right configuration, if needed' will be run during every build because it does not specify any outputs. To address this warning, either add output dependencies to the script phase, or configure it to run in every build by unchecking "Based on dependency analysis" in the script phase. (in target 'hermes-engine' from project 'Pods')
/Users/lichanghong/Documents/xxx/proj/AwesomeProject/ios/AwesomeProject.xcodeproj: error: Signing for "AwesomeProject" requires a development team. Select a development team in the Signing & Capabilities editor. (in target 'AwesomeProject' from project 'AwesomeProject')
warning: Run script build phase 'Bundle React Native code and images' will be run during every build because it does not specify any outputs. To address this warning, either add output dependencies to the script phase, or configure it to run in every build by unchecking "Based on dependency analysis" in the script phase. (in target 'AwesomeProject' from project 'AwesomeProject')
** BUILD FAILED **
关键信息是这句:"AwesomeProject" requires a development team 就是工程证书配置问题,相信只有做iOS的才能有所领悟
如上图,自动签名,选一个Team即可
大功告成
终于成功了:
然后大家可以修改一些工程内容,体验一下