Skip to content

Commit c42884f

Browse files
committed
feat: Implement UIScene lifecycle for iOS 13+
1 parent 4bb24e2 commit c42884f

5 files changed

Lines changed: 122 additions & 0 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ __attribute__((deprecated(
8181

8282
- (RCTRootViewFactory *)rootViewFactory;
8383

84+
- (UISceneConfiguration *)application:(UIApplication *)application
85+
configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession
86+
options:(UISceneConnectionOptions *)options API_AVAILABLE(ios(13.0));
87+
8488
@end
8589

8690
NS_ASSUME_NONNULL_END

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,14 @@ - (void)setBridgeAdapter:(RCTSurfacePresenterBridgeAdapter *)bridgeAdapter
8787
self.reactNativeFactory.rootViewFactory.bridgeAdapter = bridgeAdapter;
8888
}
8989

90+
- (UISceneConfiguration *)application:(UIApplication *)application
91+
configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession
92+
options:(UISceneConnectionOptions *)options API_AVAILABLE(ios(13.0))
93+
{
94+
UISceneConfiguration *configuration = [[UISceneConfiguration alloc] initWithName:@"Default Configuration"
95+
sessionRole:connectingSceneSession.role];
96+
configuration.delegateClass = NSClassFromString(@"RCTSceneDelegate");
97+
return configuration;
98+
}
99+
90100
@end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <UIKit/UIKit.h>
9+
10+
@class RCTReactNativeFactory;
11+
12+
NS_ASSUME_NONNULL_BEGIN
13+
14+
API_AVAILABLE(ios(13.0))
15+
@interface RCTSceneDelegate : UIResponder <UIWindowSceneDelegate>
16+
17+
@property (nonatomic, strong) UIWindow *window;
18+
19+
@end
20+
21+
NS_ASSUME_NONNULL_END
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import "RCTSceneDelegate.h"
9+
#import <UIKit/UIKit.h>
10+
#import "RCTAppDelegate.h"
11+
#import "RCTReactNativeFactory.h"
12+
#import "RCTRootViewFactory.h"
13+
14+
@implementation RCTSceneDelegate
15+
16+
- (void)scene:(UIScene *)scene
17+
willConnectToSession:(UISceneSession *)session
18+
options:(UISceneConnectionOptions *)connectionOptions API_AVAILABLE(ios(13.0))
19+
{
20+
if (![scene isKindOfClass:[UIWindowScene class]]) {
21+
return;
22+
}
23+
24+
UIWindowScene *windowScene = (UIWindowScene *)scene;
25+
self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
26+
27+
// Get the app delegate to access the React Native factory
28+
RCTAppDelegate *appDelegate = (RCTAppDelegate *)[UIApplication sharedApplication].delegate;
29+
30+
if (appDelegate && [appDelegate respondsToSelector:@selector(rootViewFactory)]) {
31+
RCTRootViewFactory *rootViewFactory = appDelegate.rootViewFactory;
32+
33+
UIView *rootView = [rootViewFactory viewWithModuleName:appDelegate.moduleName
34+
initialProperties:appDelegate.initialProps
35+
launchOptions:nil];
36+
37+
UIViewController *rootViewController = [UIViewController new];
38+
rootViewController.view = rootView;
39+
self.window.rootViewController = rootViewController;
40+
}
41+
42+
[self.window makeKeyAndVisible];
43+
}
44+
45+
- (void)sceneDidDisconnect:(UIScene *)scene API_AVAILABLE(ios(13.0))
46+
{
47+
// Scene disconnected
48+
}
49+
50+
- (void)sceneDidBecomeActive:(UIScene *)scene API_AVAILABLE(ios(13.0))
51+
{
52+
// Scene became active
53+
}
54+
55+
- (void)sceneWillResignActive:(UIScene *)scene API_AVAILABLE(ios(13.0))
56+
{
57+
// Scene will resign active
58+
}
59+
60+
- (void)sceneWillEnterForeground:(UIScene *)scene API_AVAILABLE(ios(13.0))
61+
{
62+
// Scene will enter foreground
63+
}
64+
65+
- (void)sceneDidEnterBackground:(UIScene *)scene API_AVAILABLE(ios(13.0))
66+
{
67+
// Scene entered background
68+
}
69+
70+
@end

packages/rn-tester/RNTester/Info.plist

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,22 @@
6363
</array>
6464
<key>UIViewControllerBasedStatusBarAppearance</key>
6565
<false/>
66+
<key>UIApplicationSceneManifest</key>
67+
<dict>
68+
<key>UIApplicationSupportsMultipleScenes</key>
69+
<false/>
70+
<key>UISceneConfigurations</key>
71+
<dict>
72+
<key>UIWindowSceneSessionRoleApplication</key>
73+
<array>
74+
<dict>
75+
<key>UISceneConfigurationName</key>
76+
<string>Default Configuration</string>
77+
<key>UISceneDelegateClassName</key>
78+
<string>RCTSceneDelegate</string>
79+
</dict>
80+
</array>
81+
</dict>
82+
</dict>
6683
</dict>
6784
</plist>

0 commit comments

Comments
 (0)