react-native-brownfield icon indicating copy to clipboard operation
react-native-brownfield copied to clipboard

SwiftUI example missing AppDelegate causes crash

Open lucabattistini opened this issue 1 year ago • 4 comments

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

  1. Create a React Native app with:
    pnpm create rock
    
  2. Create the host app in Xcode.
  3. Set up brownfield integration as shown in the docs.
  4. 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.

lucabattistini avatar Aug 19 '25 12:08 lucabattistini

Hey, which React Native version are you using? I think this should be fixed in one of the latest versions.

okwasniewski avatar Sep 08 '25 08:09 okwasniewski

It's "react-native": "0.80.1". I'll try with the latest and let you know.

lucabattistini avatar Sep 09 '25 08:09 lucabattistini

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 :)

thymikee avatar Sep 09 '25 08:09 thymikee

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

25harsh avatar Sep 11 '25 23:09 25harsh

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

artus9033 avatar Mar 20 '26 10:03 artus9033