From 4e4a92c2a3824fb15bdfaa907c72126a7e0adc2d Mon Sep 17 00:00:00 2001 From: Artur Gurgul Date: Sat, 2 Aug 2025 13:00:06 +0200 Subject: [PATCH] Hello for SwiftUI --- ios/Native/Application/RootView.swift | 24 +++++++++++++++ ios/Native/Application/SharedState.swift | 16 ++++++++++ .../Application/UIReactNativeView.swift | 29 +++++++++++++++++++ .../UIReactNativeHostingViewController.swift | 29 ------------------- ios/RNPlayground/AppDelegate.swift | 10 ++++--- 5 files changed, 75 insertions(+), 33 deletions(-) create mode 100644 ios/Native/Application/RootView.swift create mode 100644 ios/Native/Application/SharedState.swift create mode 100644 ios/Native/Application/UIReactNativeView.swift delete mode 100644 ios/Native/UIReactNativeHostingViewController.swift diff --git a/ios/Native/Application/RootView.swift b/ios/Native/Application/RootView.swift new file mode 100644 index 0000000..c13bc1e --- /dev/null +++ b/ios/Native/Application/RootView.swift @@ -0,0 +1,24 @@ +// +// RootView.swift +// RNPlayground +// +// Created by Artur Gurgul on 02/08/2025. +// + +import SwiftUI + +struct RootView: View { + private let sharedState: SharedState + init(_ sharedState: SharedState) { + self.sharedState = sharedState + } + + var body: some View { + VStack { + Text("Hello to SwiftUI!") + if let reactNativeFactory = sharedState.reactNativeFactory { + UIReactNativeView(reactNativeFactory) + } + } + } +} diff --git a/ios/Native/Application/SharedState.swift b/ios/Native/Application/SharedState.swift new file mode 100644 index 0000000..b491d6e --- /dev/null +++ b/ios/Native/Application/SharedState.swift @@ -0,0 +1,16 @@ +// +// SharedState.swift +// RNPlayground +// +// Created by Artur Gurgul on 02/08/2025. +// + +import SwiftUI +import Combine +import React +import React_RCTAppDelegate +import ReactAppDependencyProvider + +final class SharedState: ObservableObject { + var reactNativeFactory: RCTReactNativeFactory? +} diff --git a/ios/Native/Application/UIReactNativeView.swift b/ios/Native/Application/UIReactNativeView.swift new file mode 100644 index 0000000..5ff796f --- /dev/null +++ b/ios/Native/Application/UIReactNativeView.swift @@ -0,0 +1,29 @@ +// +// UIReactNativeView.swift +// RNPlayground +// +// Created by Artur Gurgul on 01/08/2025. +// + +import UIKit +import SwiftUI +import React +import React_RCTAppDelegate +import ReactAppDependencyProvider + + +struct UIReactNativeView: UIViewRepresentable { + var factory: RCTReactNativeFactory + + init(_ reactNativeFactory: RCTReactNativeFactory) { + factory = reactNativeFactory + } + + func makeUIView(context: Context) -> UIView { + return factory.rootViewFactory.view(withModuleName: "RNPlayground") + } + + func updateUIView(_ uiView: UIView, context: Context) { + + } +} diff --git a/ios/Native/UIReactNativeHostingViewController.swift b/ios/Native/UIReactNativeHostingViewController.swift deleted file mode 100644 index fd46ee2..0000000 --- a/ios/Native/UIReactNativeHostingViewController.swift +++ /dev/null @@ -1,29 +0,0 @@ -// -// UIReactNativeHostingViewController.swift -// RNPlayground -// -// Created by Artur Gurgul on 01/08/2025. -// - -import UIKit -import React -import React_RCTAppDelegate -import ReactAppDependencyProvider - -class UIReactNativeHostingViewController: UIViewController { - - var factory: RCTReactNativeFactory - - init(_ reactNativeFactory: RCTReactNativeFactory) { - self.factory = reactNativeFactory - super.init(nibName: nil, bundle: nil) - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - override func loadView() { - view = factory.rootViewFactory.view(withModuleName: "RNPlayground") - } -} diff --git a/ios/RNPlayground/AppDelegate.swift b/ios/RNPlayground/AppDelegate.swift index 0bbe441..82a2d95 100644 --- a/ios/RNPlayground/AppDelegate.swift +++ b/ios/RNPlayground/AppDelegate.swift @@ -1,4 +1,5 @@ import UIKit +import SwiftUI import React import React_RCTAppDelegate import ReactAppDependencyProvider @@ -8,7 +9,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var reactNativeDelegate: ReactNativeDelegate? - var reactNativeFactory: RCTReactNativeFactory? + private let sharedState = SharedState() func application( _ application: UIApplication, @@ -19,7 +20,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { delegate.dependencyProvider = RCTAppDependencyProvider() reactNativeDelegate = delegate - reactNativeFactory = factory + sharedState.reactNativeFactory = factory window = UIWindow(frame: UIScreen.main.bounds) @@ -34,10 +35,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } func startNativeApp() { - guard let window, let reactNativeFactory else { + guard let window, sharedState.reactNativeFactory != nil else { return } - window.rootViewController = UIReactNativeHostingViewController(reactNativeFactory) + + window.rootViewController = UIHostingController(rootView: RootView(sharedState)) window.makeKeyAndVisible() } }