React Native Enterprise Framework helps you package your React Native code into files that your iOS and Android apps can use. For iOS, it creates a .xcframework
file that you can easily add to your app.
To add React Native to your iOS app, we'll package your React Native code into an XCFramework. This way, you don't need to set up Node.js or CocoaPods in your main app.
To make the integration simpler and more powerful, we'll use the React Native Brownfield library.
If you are creating a new RNEF project, you can select the brownfield-ios
plugin to add brownfield capabilities, install dependencies, and jump to 6. Create the XCFramework step.
If you have an existing RNEF project, follow the instructions below.
Open your React Native project's ios/<project_name>.xcworkspace
in Xcode
Add a new target by clicking File > New > Target
Choose the Framework
template
Give your framework a unique name. You'll use this name when adding it to your main app
Right-click the framework folder and select Convert to Group
. CocoaPods doesn't work properly with references. Perform this step for both <FrameworkName>
and <FrameworkName>Tests
folders.
Set these build settings for your framework:
Build Setting | Value | What it does |
---|---|---|
Build Libraries for Distribution | YES | Creates a module interface for Swift. Also checks if the framework works with your Xcode version. |
User Script Sandboxing | NO | Lets scripts modify files, which we need to create the JavaScript bundle. |
Skip Install | NO | Makes sure Xcode creates the framework files we need. |
Enable Module Verifier | NO | Skips testing the framework during build, which makes builds faster. |
Add your new framework to ios/Podfile
:
Bundle React Native code and images
step
New Run Script Phase
Bundle React Native code and images
$(SRCROOT)/.xcode.env.local
$(SRCROOT)/.xcode.env
Add @rnef/plugin-brownfield-ios
to your React Native project
Add this to your rnef.config.mjs
:
Run the following command in Terminal:
Open the .rnef/cache/ios/package
directory and drag the following files into your app's Xcode project (you can select all at once):
Xcode builds depend on files being referenced in the project file. If you create a new source file without using Xcode, your file is not going to be referenced by the project.
Initialize React Native in AppDelegate.swift
and show it using ReactNativeViewController
available from React Native Brownfield library:
Instead of using AppDelegate
you can also use SceneDelegate
Now that you have everything set up, you can run your app in Debug or Release configuration, and it will display a React Native app we just packaged with package:ios
command.
When running in Debug, React Native Brownfield will expect you to run a JS dev server.
You can do so by running start
command in your Terminal app:
When running in Release, React Native Brownfield will load the JS bundle (main.jsbundle
) directly from the release XCFramework file without requiring you to run any JavaScript tooling.