1- import React
2- import React_RCTAppDelegate
3- import ReactAppDependencyProvider
1+ import UIKit
2+ internal import React
3+ internal import React_RCTAppDelegate
4+ internal import ReactAppDependencyProvider
45
5- @objc public class ReactNativeBrownfield : RCTDefaultReactNativeFactoryDelegate {
6- @objc public static let shared = ReactNativeBrownfield ( )
7- private var onBundleLoaded : ( ( ) -> Void ) ?
6+ class ReactNativeBrownfieldDelegate : RCTDefaultReactNativeFactoryDelegate {
7+ var entryFile = " index "
8+ // MARK: - RCTReactNativeFactoryDelegate Methods
9+
10+ override func sourceURL( for bridge: RCTBridge ) -> URL ? {
11+ return bundleURL ( )
12+ }
13+
14+ public override func bundleURL( ) -> URL ? {
15+ #if DEBUG
16+ return RCTBundleURLProvider . sharedSettings ( ) . jsBundleURL ( forBundleRoot: entryFile)
17+ #else
18+ let resourceURLComponents = bundlePath. components ( separatedBy: " . " )
19+ let withoutLast = resourceURLComponents [ ..< ( resourceURLComponents. count - 1 ) ]
20+ let resourceName = withoutLast. joined ( )
21+ let fileExtension = resourceURLComponents. last ?? " "
22+
23+ return Bundle . main. url ( forResource: resourceName, withExtension: fileExtension)
24+ #endif
25+ }
26+ }
827
28+ @objc public class ReactNativeBrownfield : NSObject {
29+ public static let shared = ReactNativeBrownfield ( )
30+ private var onBundleLoaded : ( ( ) -> Void ) ?
31+ private var delegate = ReactNativeBrownfieldDelegate ( )
32+
933 /**
1034 * Path to JavaScript root.
1135 * Default value: "index"
1236 */
13- @objc public var entryFile : String = " index "
37+ @objc public var entryFile : String = " index " {
38+ didSet {
39+ delegate. entryFile = entryFile
40+ }
41+ }
1442 /**
1543 * Path to bundle fallback resource.
1644 * Default value: nil
@@ -25,24 +53,36 @@ import ReactAppDependencyProvider
2553 * React Native factory instance created when starting React Native.
2654 * Default value: nil
2755 */
28- @ objc public var reactNativeFactory : RCTReactNativeFactory ? = nil
56+ private var reactNativeFactory : RCTReactNativeFactory ? = nil
2957 /**
3058 * Root view factory used to create React Native views.
3159 */
32- @ objc lazy public var rootViewFactory : RCTRootViewFactory ? = {
60+ lazy private var rootViewFactory : RCTRootViewFactory ? = {
3361 return reactNativeFactory? . rootViewFactory
3462 } ( )
35-
63+
3664 /**
3765 * Starts React Native with default parameters.
3866 */
3967 @objc public func startReactNative( ) {
4068 startReactNative ( onBundleLoaded: nil )
4169 }
4270
71+ @objc public func view(
72+ moduleName: String ,
73+ initialProps: [ AnyHashable : Any ] ? ,
74+ launchOptions: [ AnyHashable : Any ] ? = nil
75+ ) -> UIView ? {
76+ reactNativeFactory? . rootViewFactory. view (
77+ withModuleName: moduleName,
78+ initialProperties: initialProps,
79+ launchOptions: launchOptions
80+ )
81+ }
82+
4383 /**
4484 * Starts React Native with optional callback when bundle is loaded.
45- *
85+ *
4686 * @param onBundleLoaded Optional callback invoked after JS bundle is fully loaded.
4787 */
4888 @objc public func startReactNative( onBundleLoaded: ( ( ) -> Void ) ? ) {
@@ -51,15 +91,15 @@ import ReactAppDependencyProvider
5191
5292 /**
5393 * Starts React Native with optional callback and launch options.
54- *
94+ *
5595 * @param onBundleLoaded Optional callback invoked after JS bundle is fully loaded.
5696 * @param launchOptions Launch options, typically passed from AppDelegate.
5797 */
5898 @objc public func startReactNative( onBundleLoaded: ( ( ) -> Void ) ? , launchOptions: [ AnyHashable : Any ] ? ) {
5999 guard reactNativeFactory == nil else { return }
60100
61- self . dependencyProvider = RCTAppDependencyProvider ( )
62- self . reactNativeFactory = RCTReactNativeFactory ( delegate: self )
101+ delegate . dependencyProvider = RCTAppDependencyProvider ( )
102+ self . reactNativeFactory = RCTReactNativeFactory ( delegate: delegate )
63103
64104 if let onBundleLoaded {
65105 self . onBundleLoaded = onBundleLoaded
@@ -86,25 +126,6 @@ import ReactAppDependencyProvider
86126 onBundleLoaded = nil
87127 NotificationCenter . default. removeObserver ( self )
88128 }
89-
90- // MARK: - RCTReactNativeFactoryDelegate Methods
91-
92- @objc public override func sourceURL( for bridge: RCTBridge ) -> URL ? {
93- return bundleURL ( )
94- }
95-
96- public override func bundleURL( ) -> URL ? {
97- #if DEBUG
98- return RCTBundleURLProvider . sharedSettings ( ) . jsBundleURL ( forBundleRoot: entryFile)
99- #else
100- let resourceURLComponents = bundlePath. components ( separatedBy: " . " )
101- let withoutLast = resourceURLComponents [ ..< ( resourceURLComponents. count - 1 ) ]
102- let resourceName = withoutLast. joined ( )
103- let fileExtension = resourceURLComponents. last ?? " "
104-
105- return Bundle . main. url ( forResource: resourceName, withExtension: fileExtension)
106- #endif
107- }
108129}
109130
110131extension Notification . Name {
0 commit comments