Skip to content

SwiftUI example missing AppDelegate causes crash #127

@lucabattistini

Description

@lucabattistini

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions