Skip to content

Commit 76e3d6a

Browse files
committed
fix: dimensions, use RCTMainWindow()
1 parent 4a0600d commit 76e3d6a

File tree

15 files changed

+133
-82
lines changed

15 files changed

+133
-82
lines changed

packages/react-native/Libraries/AppDelegate/RCTAppDelegate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ NS_ASSUME_NONNULL_BEGIN
159159
- (BOOL)bridgelessEnabled;
160160

161161
/// Return the bundle URL for the main bundle.
162-
- (NSURL *)bundleURL;
162+
- (NSURL *__nullable)bundleURL;
163163

164164
/// Don't use this method, it's going to be removed soon.
165165
- (UIView *)viewWithModuleName:(NSString *)moduleName initialProperties:(NSDictionary*)initialProperties launchOptions:(NSDictionary*)launchOptions;

packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ - (Class)getModuleClassFromName:(const char *)name
312312

313313
- (void)createReactHost
314314
{
315+
if (_reactHost != nil) {
316+
return;
317+
}
315318
__weak __typeof(self) weakSelf = self;
316319
_reactHost = [[RCTHost alloc] initWithBundleURL:[self bundleURL]
317320
hostDelegate:nil
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import SwiftUI
2+
3+
/**
4+
This SwiftUI struct returns main React Native scene. It should be used only once as it conains setup code.
5+
6+
Example:
7+
```swift
8+
@main
9+
struct YourApp: App {
10+
@UIApplicationDelegateAdaptor var delegate: AppDelegate
11+
12+
var body: some Scene {
13+
RCTMainWindow(moduleName: "YourApp")
14+
}
15+
}
16+
```
17+
18+
Note: If you want to create additional windows in your app, create a new `WindowGroup {}` and pass it a `RCTRootViewRepresentable`.
19+
*/
20+
public struct RCTMainWindow: Scene {
21+
var moduleName: String
22+
var initialProps: RCTRootViewRepresentable.InitialPropsType
23+
24+
public init(moduleName: String, initialProps: RCTRootViewRepresentable.InitialPropsType = nil) {
25+
self.moduleName = moduleName
26+
self.initialProps = initialProps
27+
}
28+
29+
public var body: some Scene {
30+
WindowGroup {
31+
RCTRootViewRepresentable(moduleName: moduleName, initialProps: initialProps)
32+
}
33+
}
34+
}

packages/react-native/Libraries/SwiftExtensions/RCTReactViewController.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#import <UIKit/UIKit.h>
22

3+
/**
4+
A `UIViewController` responsible for embeding `RCTRootView` inside. Uses Factory pattern to retrive new view instances.
5+
6+
Note: Used to in `RCTRootViewRepresentable` to display React views.
7+
*/
38
@interface RCTReactViewController : UIViewController
49

5-
@property (nonatomic, strong) NSString *_Nonnull moduleName;
6-
@property (nonatomic, strong) NSDictionary *_Nullable initialProps;
10+
@property (nonatomic, strong, nonnull) NSString *moduleName;
11+
@property (nonatomic, strong, nullable) NSDictionary *initialProps;
712

813
- (instancetype _Nonnull)initWithModuleName:(NSString *_Nonnull)moduleName
914
initProps:(NSDictionary *_Nullable)initProps;

packages/react-native/Libraries/SwiftExtensions/RCTReactViewController.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#import "RCTReactViewController.h"
2+
#import <React/RCTConstants.h>
23

34
@protocol RCTRootViewFactoryProtocol <NSObject>
45

@@ -16,6 +17,10 @@ - (instancetype)initWithModuleName:(NSString *)moduleName initProps:(NSDictionar
1617
return self;
1718
}
1819

20+
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
21+
[[NSNotificationCenter defaultCenter] postNotificationName:RCTWindowFrameDidChangeNotification object:self];
22+
}
23+
1924
// TODO: Temporary solution for creating RCTRootView on demand. This should be done through factory pattern, see here: https://github.com/facebook/react-native/pull/42263
2025
- (void)loadView {
2126
id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;

packages/react-native/Libraries/SwiftExtensions/RCTRootViewRepresentable.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
import SwiftUI
22

3-
/*
4-
* Use this struct in SwiftUI context to present React Native rootView.
5-
*
6-
* Example:
7-
* ```
8-
* WindowGroup {
9-
* RCTRootViewRepresentable(moduleName: "RNTesterApp", initialProps: [:])
10-
* }
11-
* ```
12-
*/
3+
/**
4+
SwiftUI view enclosing `RCTReactViewController`. Its main purpose is to display React Native views inside of SwiftUI lifecycle.
5+
6+
Use it create new windows in your app:
7+
Example:
8+
```swift
9+
WindowGroup {
10+
RCTRootViewRepresentable(moduleName: "YourAppName", initialProps: [:])
11+
}
12+
```
13+
*/
1314
public struct RCTRootViewRepresentable: UIViewControllerRepresentable {
15+
public typealias InitialPropsType = [AnyHashable: Any]?
16+
1417
var moduleName: String
15-
var initialProps: [AnyHashable: Any]?
18+
var initialProps: InitialPropsType
1619

17-
public init(moduleName: String, initialProps: [AnyHashable : Any]?) {
20+
public init(moduleName: String, initialProps: InitialPropsType = nil) {
1821
self.moduleName = moduleName
1922
self.initialProps = initialProps
2023
}

packages/react-native/Libraries/SwiftExtensions/React-RCTSwiftExtensions.podspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ Pod::Spec.new do |s|
2222
s.source = source
2323
s.source_files = "*.{swift,h,m}"
2424
s.frameworks = ["UIKit", "SwiftUI"]
25+
26+
s.dependency "React-Core"
2527
end

packages/react-native/React/Base/RCTBridgeDelegate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ NS_ASSUME_NONNULL_BEGIN
2020
* When running from a locally bundled JS file, this should be a `file://` url
2121
* pointing to a path inside the app resources, e.g. `file://.../main.jsbundle`.
2222
*/
23-
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge;
23+
- (NSURL *__nullable)sourceURLForBridge:(RCTBridge *)bridge;
2424

2525
@optional
2626

packages/react-native/template/_gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,4 @@ yarn-error.log
6565

6666
# testing
6767
/coverage
68+
.yarn

packages/react-native/template/visionos/HelloWorld.xcodeproj/project.pbxproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
00E356F31AD99517003FC87E /* HelloWorldTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 00E356F21AD99517003FC87E /* HelloWorldTests.m */; };
1111
0C80B921A6F3F58F76C31292 /* libPods-HelloWorld.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 5DCACB8F33CDC322A6C60F78 /* libPods-HelloWorld.a */; };
1212
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; };
13-
767CEB962B56C0F3000139AD /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 767CEB952B56C0F3000139AD /* AppDelegate.swift */; };
14-
767CEB982B56C0FA000139AD /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 767CEB972B56C0FA000139AD /* App.swift */; };
13+
767CEBBC2B582F6B000139AD /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 767CEBBB2B582F6B000139AD /* AppDelegate.swift */; };
14+
767CEBBE2B582F78000139AD /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 767CEBBD2B582F78000139AD /* App.swift */; };
1515
7699B88040F8A987B510C191 /* libPods-HelloWorld-HelloWorldTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 19F6CBCC0A4E27FBF8BF4A61 /* libPods-HelloWorld-HelloWorldTests.a */; };
1616
/* End PBXBuildFile section */
1717

@@ -37,8 +37,8 @@
3737
5709B34CF0A7D63546082F79 /* Pods-HelloWorld.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HelloWorld.release.xcconfig"; path = "Target Support Files/Pods-HelloWorld/Pods-HelloWorld.release.xcconfig"; sourceTree = "<group>"; };
3838
5B7EB9410499542E8C5724F5 /* Pods-HelloWorld-HelloWorldTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HelloWorld-HelloWorldTests.debug.xcconfig"; path = "Target Support Files/Pods-HelloWorld-HelloWorldTests/Pods-HelloWorld-HelloWorldTests.debug.xcconfig"; sourceTree = "<group>"; };
3939
5DCACB8F33CDC322A6C60F78 /* libPods-HelloWorld.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-HelloWorld.a"; sourceTree = BUILT_PRODUCTS_DIR; };
40-
767CEB952B56C0F3000139AD /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AppDelegate.swift; path = VisionAppTestSUI/AppDelegate.swift; sourceTree = "<group>"; };
41-
767CEB972B56C0FA000139AD /* App.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = App.swift; path = VisionAppTestSUI/App.swift; sourceTree = "<group>"; };
40+
767CEBBB2B582F6B000139AD /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AppDelegate.swift; path = HelloWorld/AppDelegate.swift; sourceTree = "<group>"; };
41+
767CEBBD2B582F78000139AD /* App.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = App.swift; path = HelloWorld/App.swift; sourceTree = "<group>"; };
4242
89C6BE57DB24E9ADA2F236DE /* Pods-HelloWorld-HelloWorldTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HelloWorld-HelloWorldTests.release.xcconfig"; path = "Target Support Files/Pods-HelloWorld-HelloWorldTests/Pods-HelloWorld-HelloWorldTests.release.xcconfig"; sourceTree = "<group>"; };
4343
ED297162215061F000B7C4FE /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; };
4444
/* End PBXFileReference section */
@@ -83,8 +83,8 @@
8383
13B07FAE1A68108700A75B9A /* HelloWorld */ = {
8484
isa = PBXGroup;
8585
children = (
86-
767CEB952B56C0F3000139AD /* AppDelegate.swift */,
87-
767CEB972B56C0FA000139AD /* App.swift */,
86+
767CEBBB2B582F6B000139AD /* AppDelegate.swift */,
87+
767CEBBD2B582F78000139AD /* App.swift */,
8888
13B07FB51A68108700A75B9A /* Images.xcassets */,
8989
13B07FB61A68108700A75B9A /* Info.plist */,
9090
);
@@ -386,8 +386,8 @@
386386
isa = PBXSourcesBuildPhase;
387387
buildActionMask = 2147483647;
388388
files = (
389-
767CEB962B56C0F3000139AD /* AppDelegate.swift in Sources */,
390-
767CEB982B56C0FA000139AD /* App.swift in Sources */,
389+
767CEBBC2B582F6B000139AD /* AppDelegate.swift in Sources */,
390+
767CEBBE2B582F78000139AD /* App.swift in Sources */,
391391
);
392392
runOnlyForDeploymentPostprocessing = 0;
393393
};

0 commit comments

Comments
 (0)