-
Notifications
You must be signed in to change notification settings - Fork 26
Description
Environment
- Platform: iOS
- Framework: SwiftUI
- Project Setup: Third-party app created with Xcode, React Native app generated with
rock
CLI - 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.