Skip to content

Commit ba337b8

Browse files
authored
fix(example): adopt iOS scene lifecycle (#138)
1 parent 64957b2 commit ba337b8

4 files changed

Lines changed: 86 additions & 8 deletions

File tree

‎example/ios/GraphExample.xcodeproj/project.pbxproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
2EA65936A4AAD5DBC0E90976 /* ExpoModulesProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60AB8250B427F8B90CFECE45 /* ExpoModulesProvider.swift */; };
1212
3E461D99554A48A4959DE609 /* SplashScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AA286B85B6C04FC6940260E9 /* SplashScreen.storyboard */; };
1313
A6477CFC522B154B85A02AF8 /* libPods-GraphExample.a in Frameworks */ = {isa = PBXBuildFile; fileRef = B83B84440E7A3A5D12FCA110 /* libPods-GraphExample.a */; };
14+
A7EB6D212DCB00010003AF01 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7EB6D202DCB00010003AF01 /* SceneDelegate.swift */; };
1415
B8F1574668A4582978DB4C6D /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 10BF3EA0640E000BC2678227 /* PrivacyInfo.xcprivacy */; };
1516
BB2F792D24A3F905000567C9 /* Expo.plist in Resources */ = {isa = PBXBuildFile; fileRef = BB2F792C24A3F905000567C9 /* Expo.plist */; };
1617
F11748422D0307B40044C1D9 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F11748412D0307B40044C1D9 /* AppDelegate.swift */; };
@@ -22,6 +23,7 @@
2223
13B07FB51A68108700A75B9A /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Images.xcassets; path = GraphExample/Images.xcassets; sourceTree = "<group>"; };
2324
13B07FB61A68108700A75B9A /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = GraphExample/Info.plist; sourceTree = "<group>"; };
2425
60AB8250B427F8B90CFECE45 /* ExpoModulesProvider.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ExpoModulesProvider.swift; path = "Pods/Target Support Files/Pods-GraphExample/ExpoModulesProvider.swift"; sourceTree = "<group>"; };
26+
A7EB6D202DCB00010003AF01 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = SceneDelegate.swift; path = GraphExample/SceneDelegate.swift; sourceTree = "<group>"; };
2527
AA286B85B6C04FC6940260E9 /* SplashScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = SplashScreen.storyboard; path = GraphExample/SplashScreen.storyboard; sourceTree = "<group>"; };
2628
B83B84440E7A3A5D12FCA110 /* libPods-GraphExample.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-GraphExample.a"; sourceTree = BUILT_PRODUCTS_DIR; };
2729
BB2F792C24A3F905000567C9 /* Expo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Expo.plist; sourceTree = "<group>"; };
@@ -48,6 +50,7 @@
4850
isa = PBXGroup;
4951
children = (
5052
F11748412D0307B40044C1D9 /* AppDelegate.swift */,
53+
A7EB6D202DCB00010003AF01 /* SceneDelegate.swift */,
5154
F11748442D0722820044C1D9 /* GraphExample-Bridging-Header.h */,
5255
BB2F792B24A3F905000567C9 /* Supporting */,
5356
13B07FB51A68108700A75B9A /* Images.xcassets */,
@@ -322,6 +325,7 @@
322325
buildActionMask = 2147483647;
323326
files = (
324327
F11748422D0307B40044C1D9 /* AppDelegate.swift in Sources */,
328+
A7EB6D212DCB00010003AF01 /* SceneDelegate.swift in Sources */,
325329
2EA65936A4AAD5DBC0E90976 /* ExpoModulesProvider.swift in Sources */,
326330
);
327331
runOnlyForDeploymentPostprocessing = 0;

‎example/ios/GraphExample/AppDelegate.swift‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class AppDelegate: ExpoAppDelegate {
88

99
var reactNativeDelegate: ExpoReactNativeFactoryDelegate?
1010
var reactNativeFactory: RCTReactNativeFactory?
11+
private(set) var launchOptions: [UIApplication.LaunchOptionsKey: Any]?
1112

1213
public override func application(
1314
_ application: UIApplication,
@@ -19,14 +20,7 @@ class AppDelegate: ExpoAppDelegate {
1920

2021
reactNativeDelegate = delegate
2122
reactNativeFactory = factory
22-
23-
#if os(iOS) || os(tvOS)
24-
window = UIWindow(frame: UIScreen.main.bounds)
25-
factory.startReactNative(
26-
withModuleName: "main",
27-
in: window,
28-
launchOptions: launchOptions)
29-
#endif
23+
self.launchOptions = launchOptions
3024

3125
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
3226
}

‎example/ios/GraphExample/Info.plist‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@
4646
</dict>
4747
<key>RCTNewArchEnabled</key>
4848
<true/>
49+
<key>UIApplicationSceneManifest</key>
50+
<dict>
51+
<key>UIApplicationSupportsMultipleScenes</key>
52+
<false/>
53+
<key>UISceneConfigurations</key>
54+
<dict>
55+
<key>UIWindowSceneSessionRoleApplication</key>
56+
<array>
57+
<dict>
58+
<key>UISceneConfigurationName</key>
59+
<string>Default Configuration</string>
60+
<key>UISceneDelegateClassName</key>
61+
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
62+
</dict>
63+
</array>
64+
</dict>
65+
</dict>
4966
<key>UILaunchStoryboardName</key>
5067
<string>SplashScreen</string>
5168
<key>UIRequiredDeviceCapabilities</key>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import UIKit
2+
import React_RCTAppDelegate
3+
4+
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
5+
var window: UIWindow?
6+
7+
func scene(
8+
_ scene: UIScene,
9+
willConnectTo session: UISceneSession,
10+
options connectionOptions: UIScene.ConnectionOptions
11+
) {
12+
guard let windowScene = scene as? UIWindowScene else { return }
13+
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate,
14+
let factory = appDelegate.reactNativeFactory else {
15+
assertionFailure("React Native must be initialized before connecting a scene")
16+
return
17+
}
18+
19+
let window = UIWindow(windowScene: windowScene)
20+
self.window = window
21+
appDelegate.window = window
22+
23+
var launchOptions = appDelegate.launchOptions ?? [:]
24+
if let url = connectionOptions.urlContexts.first?.url {
25+
launchOptions[.url] = url
26+
}
27+
28+
factory.startReactNative(
29+
withModuleName: "main",
30+
in: window,
31+
launchOptions: launchOptions
32+
)
33+
}
34+
35+
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
36+
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
37+
38+
for context in URLContexts {
39+
_ = appDelegate.application(UIApplication.shared, open: context.url)
40+
}
41+
}
42+
43+
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
44+
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
45+
_ = appDelegate.application(UIApplication.shared, continue: userActivity) { _ in }
46+
}
47+
48+
func sceneDidBecomeActive(_ scene: UIScene) {
49+
(UIApplication.shared.delegate as? AppDelegate)?.applicationDidBecomeActive(UIApplication.shared)
50+
}
51+
52+
func sceneWillResignActive(_ scene: UIScene) {
53+
(UIApplication.shared.delegate as? AppDelegate)?.applicationWillResignActive(UIApplication.shared)
54+
}
55+
56+
func sceneDidEnterBackground(_ scene: UIScene) {
57+
(UIApplication.shared.delegate as? AppDelegate)?.applicationDidEnterBackground(UIApplication.shared)
58+
}
59+
60+
func sceneWillEnterForeground(_ scene: UIScene) {
61+
(UIApplication.shared.delegate as? AppDelegate)?.applicationWillEnterForeground(UIApplication.shared)
62+
}
63+
}

0 commit comments

Comments
 (0)