如何彻底移除iOS项目中的React Native,转为纯Swift项目?
Got it, let's walk through how to fully strip React Native out of your iOS project and convert it to pure Swift. I've gone through this process a few times, so here's a detailed, step-by-step breakdown that covers every corner to make sure nothing is left behind:
First up, open your project's Podfile and delete all React Native-related pods. That includes things like React, React-Core, React-NativeModules, any third-party RN libraries you were using, and the use_react_native! line if you have it.
Once that's done, run these commands in your terminal (from the iOS project directory) to clean up:
pod deintegrate pod install
This will remove all RN-related pods and rebuild your Pods directory with only the dependencies your pure Swift project needs.
Now let's fix the Xcode project itself:
- Update AppDelegate.swift: Remove all React Native initialization code. Replace the part where you set up
RCTRootViewwith your pure Swift root view controller—whether that's aUINavigationController,UIHostingController(for SwiftUI), or your custom main view controller. - Delete RN-specific resources: Remove files like
main.jsbundle, RN-onlyAssets.xcassets, and any other RN-related asset files from your Xcode project. Make sure to select "Move to Trash" when deleting to remove them from the filesystem too. - Remove RN build phases: Go to your project's target > Build Phases. Delete the "Bundle React Native code and images" phase, and any other RN-specific build phases you might have added (like copying RN assets).
- Clean Info.plist: Remove any RN-related entries, such as
NSAppTransportSecurityexceptions added for local RN debugging, or custom URL schemes used for RN deep linking (if you don't need them anymore).
Head to your project's root folder (outside the iOS directory) and delete these RN-specific files/folders:
node_modulesfolderpackage.json,yarn.lock,package-lock.jsonmetro.config.js,index.js,App.js(or your main RN entry files).babelrc,.eslintrc, or any other RN-related config files- If you have an unused
androidfolder, you can delete that too.
Even after the above steps, there might be leftover references. Do a full search in your Xcode project (use the top-right search bar, set to "Find in Project") for keywords like React, RCT, RN, or any RN library names you were using.
For each result:
- Delete import statements for RN classes (like
import Reactorimport RCTRootView) - Remove any code that uses RN APIs or components
- Check Build Settings: Go to your target > Build Settings, search for "Header Search Paths" and "Library Search Paths"—delete any paths pointing to React Native or its modules. Also, check "Other Linker Flags" and remove any flags related to RN (like
-lReact).
Finally:
- Run
Product > Clean Build Folderin Xcode to clear any cached RN build files - Build and run your project to make sure it works without crashes or errors
- Double-check your project directory with a global search tool (like VS Code) to ensure no RN-related files or code snippets were missed
Pro tip: Before you start any of this, make a full backup of your project (or commit everything to Git with a clear message like "Pre-RN removal backup")—it's easy to accidentally delete something you need!
内容的提问来源于stack exchange,提问作者iHulk

