SwiftUI example missing AppDelegate causes crash
Environment
- Platform: iOS
- Framework: SwiftUI
- Project Setup: Third-party app created with Xcode, React Native app generated with
rockCLI - Xcode: latest
- iOS: 15+
Description
The SwiftUI example in the docs is missing the required AppDelegate setup. Without it, React Native can't initialize properly. SwiftUI apps don’t use a UIKit AppDelegate by default, but React Native needs one with a window property. If you skip this, the app will crash when reopening from the home screen because RCTDeviceInfo expects the window selector.
You also need to set ReactNativeBrownfield.shared.bundle = ReactNativeBundle; otherwise, it defaults to Bundle.main, which doesn't work for brownfield setups.
Reproducible Demo
- Create a React Native app with:
pnpm create rock - Create the host app in Xcode.
- Set up brownfield integration as shown in the docs.
- Run the app, send it to the background, and reopen it.
Result:
The app crashes on reopen due to the missing window property in AppDelegate.
Solution
Add this AppDelegate:
import UIKit
import ReactBrownfield
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
return true
}
}
And in your SwiftUI app, set up React Native in init:
import SwiftUI
import ReactBrownfield
@main
struct MyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
init() {
ReactNativeBrownfield.shared.bundle = ReactNativeBundle
ReactNativeBrownfield.shared.startReactNative {
print("React Native bundle loaded")
}
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Expected:
Docs should show a complete example with both AppDelegate and bundle assignment. If you agrees, I can open a PR with the fix myself.
Hey, which React Native version are you using? I think this should be fixed in one of the latest versions.
It's "react-native": "0.80.1". I'll try with the latest and let you know.
Hey, it would be great if you could update the docs at https://www.rockjs.dev/docs/brownfield/ios 🙌🏼. If you spot anything confusing that could be put simpler, don't hesitate to update it :)
Hey, i have fixed this issue https://github.com/facebook/react-native/commit/968909488a844c695a92ce000497840e577190dd
and have raised multiple pick requests 0.79.x -> 0.81.x
Hi @lucabattistini! I revisited this issue and the docs currently include the proper setup instructions, similar to what you posted:
- https://oss.callstack.com/react-native-brownfield/docs/api-reference/brownie/swift-usage#setup
- https://oss.callstack.com/react-native-brownfield/docs/getting-started/ios#7-initialize-react-native