Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

withIosSceneLifecycle.cjs

123 lines4.1 KB

1const { withAppDelegate, withInfoPlist } = require("expo/config-plugins");
2​
3const SCENE_DELEGATE = `
4​
5class SceneDelegate: UIResponder, UIWindowSceneDelegate {
6 var window: UIWindow?
7​
8 func scene(
9 _ scene: UIScene,
10 willConnectTo session: UISceneSession,
11 options connectionOptions: UIScene.ConnectionOptions
12 ) {
13 guard
14 let windowScene = scene as? UIWindowScene,
15 let appDelegate = UIApplication.shared.delegate as? AppDelegate
16 else {
17 return
18 }
19​
20 let appWindow: UIWindow
21 if let existingWindow = appDelegate.window {
22 appWindow = existingWindow
23 } else {
24 appWindow = UIWindow(windowScene: windowScene)
25 appDelegate.window = appWindow
26 appDelegate.reactNativeFactory?.startReactNative(
27 withModuleName: "main",
28 in: appWindow,
29 launchOptions: appDelegate.sceneLaunchOptions)
30 appDelegate.sceneLaunchOptions = nil
31 }
32​
33 window = appWindow
34 appWindow.windowScene = windowScene
35 appWindow.makeKeyAndVisible()
36​
37 if let url = connectionOptions.urlContexts.first?.url {
38 _ = appDelegate.application(UIApplication.shared, open: url, options: [:])
39 }
40​
41 if let userActivity = connectionOptions.userActivities.first {
42 _ = appDelegate.application(
43 UIApplication.shared,
44 continue: userActivity,
45 restorationHandler: { _ in })
46 }
47 }
48​
49 func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
50 guard
51 let url = URLContexts.first?.url,
52 let appDelegate = UIApplication.shared.delegate as? AppDelegate
53 else {
54 return
55 }
56​
57 _ = appDelegate.application(UIApplication.shared, open: url, options: [:])
58 }
59​
60 func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
61 guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
62 return
63 }
64​
65 _ = appDelegate.application(
66 UIApplication.shared,
67 continue: userActivity,
68 restorationHandler: { _ in })
69 }
70}`;
71​
72module.exports = function withIosSceneLifecycle(config) {
73 config = withInfoPlist(config, (nextConfig) => {
74 nextConfig.modResults.UIApplicationSceneManifest = {
75 UIApplicationSupportsMultipleScenes: false,
76 UISceneConfigurations: {
77 UIWindowSceneSessionRoleApplication: [
78 {
79 UISceneConfigurationName: "Default Configuration",
80 UISceneDelegateClassName: "$(PRODUCT_MODULE_NAME).SceneDelegate",
81 },
82 ],
83 },
84 };
85​
86 return nextConfig;
87 });
88​
89 return withAppDelegate(config, (nextConfig) => {
90 if (nextConfig.modResults.language !== "swift") {
91 throw new Error("The iOS scene lifecycle plugin requires a Swift AppDelegate.");
92 }
93​
94 // Creating the window before a scene exists leaves iOS share scenes with
95 // incorrect geometry, even if windowScene is assigned afterward.
96 const startup =
97 /window = UIWindow\(frame: UIScreen\.main\.bounds\)\s+factory\.startReactNative\(\s+withModuleName: "main",\s+in: window,\s+launchOptions: launchOptions\)/;
98 if (startup.test(nextConfig.modResults.contents)) {
99 nextConfig.modResults.contents = nextConfig.modResults.contents.replace(
100 startup,
101 "sceneLaunchOptions = launchOptions",
102 );
103 } else if (!nextConfig.modResults.contents.includes("sceneLaunchOptions = launchOptions")) {
104 throw new Error("Could not move React Native startup into the iOS scene lifecycle.");
105 }
106 if (!nextConfig.modResults.contents.includes("var sceneLaunchOptions:")) {
107 nextConfig.modResults.contents = nextConfig.modResults.contents.replace(
108 "var window: UIWindow?",
109 "var window: UIWindow?\n var sceneLaunchOptions: [UIApplication.LaunchOptionsKey: Any]?",
110 );
111 }
112 if (!nextConfig.modResults.contents.includes("class SceneDelegate:")) {
113 nextConfig.modResults.contents += SCENE_DELEGATE;
114 } else {
115 nextConfig.modResults.contents = nextConfig.modResults.contents.replace(
116 "in: appWindow,\n launchOptions: nil)",
117 "in: appWindow,\n launchOptions: appDelegate.sceneLaunchOptions)\n appDelegate.sceneLaunchOptions = nil",
118 );
119 }
120​
121 return nextConfig;
122 });
123};