指定.xcworkspace执行react-native run-ios失败,现如何用Cocoapods命令行运行?
Hey there, I get that frustration—trying to specify your .xcworkspace directly with react-native run-ios only to hit that "Could not find Xcode project files" error is a bummer, especially since official support for this is still on the roadmap. But don’t worry, there are a few solid workarounds to get you running your React Native project with Cocoapods via the command line right now:
1. Use xcodebuild directly (the most straightforward command-line approach)
Skip the React Native CLI altogether and use Xcode’s native command-line tool to target your workspace. Here’s a command that builds and runs your app on the iPhone 8 Plus simulator:
xcodebuild -workspace ios/myProject.xcworkspace -scheme myProject -destination 'platform=iOS Simulator,name=iPhone 8 Plus' build && xcrun simctl install booted ios/build/Build/Products/Debug-iphonesimulator/myProject.app && xcrun simctl launch booted com.yourcompany.myProject
Just make sure to replace myProject with your actual scheme name, and com.yourcompany.myProject with your app’s bundle identifier.
2. Create a symlink to trick the React Native CLI
The React Native CLI looks for a workspace or project file in the ios directory that matches your root project’s name. If your workspace is named differently (like myProject.xcworkspace instead of [RootProjectName].xcworkspace), create a symbolic link to match what the CLI expects:
cd ios ln -s myProject.xcworkspace [YourRootProjectName].xcworkspace
Replace [YourRootProjectName] with the name of your React Native root project folder. Once you’ve done this, you can use the standard react-native run-ios command without issues:
react-native run-ios --simulator="iPhone 8 Plus"
3. Use a custom shell script for repeatability
If you want a reusable command without modifying files or dealing with symlinks, create a simple shell script in your project root. Name it something like run-ios-custom.sh:
#!/bin/bash # Build the app via the workspace xcodebuild -workspace ios/myProject.xcworkspace -scheme myProject -destination 'platform=iOS Simulator,name=iPhone 8 Plus' build # Install the app to the booted simulator xcrun simctl install booted ios/build/Build/Products/Debug-iphonesimulator/myProject.app # Launch the app xcrun simctl launch booted com.yourcompany.myProject
Then make it executable:
chmod +x run-ios-custom.sh
Now you can run your app anytime with:
./run-ios-custom.sh
All these are temporary workarounds, but they’ll get you up and running until the official --project-path support for workspaces lands.
内容的提问来源于stack exchange,提问作者Paul Brittain

