@@ -14,8 +14,6 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
1414
1515 @AppStorage ( " loggedIn " ) private var loggedIn : Bool = false
1616
17- var posmLoginView : PosmLoginView ? = nil
18-
1917 func scene( _ scene: UIScene , willConnectTo session: UISceneSession , options connectionOptions: UIScene . ConnectionOptions ) {
2018 // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
2119 // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
@@ -40,7 +38,12 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
4038 window. rootViewController = UIHostingController ( rootView: contentView)
4139 self . window = window
4240 window. makeKeyAndVisible ( )
43- self . posmLoginView = contentView
41+ }
42+
43+ if let userActivity = connectionOptions. userActivities. first,
44+ userActivity. activityType == NSUserActivityTypeBrowsingWeb,
45+ let incomingURL = userActivity. webpageURL {
46+ handleUniversalLink ( incomingURL)
4447 }
4548 }
4649 }
@@ -72,34 +75,44 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
7275 // Use this method to save data, release shared resources, and store enough scene-specific state information
7376 // to restore the scene back to its current state.
7477 }
78+
79+ func scene( _ scene: UIScene , continue userActivity: NSUserActivity ) {
80+ guard userActivity. activityType == NSUserActivityTypeBrowsingWeb,
81+ let incomingURL = userActivity. webpageURL else {
82+ return
83+ }
84+
85+ handleUniversalLink ( incomingURL)
86+ }
87+
88+ private func handleUniversalLink( _ url: URL ) {
89+ print ( " Universal Link called " )
90+ guard !loggedIn else {
91+ debugPrint ( " user is already login " )
92+ return // user is already login
93+ }
94+
95+ guard let refreshToken = url. queryParameters ? [ " code " ] else {
96+ debugPrint ( " invalid scheme url format " )
97+ return // invalid url format
98+ }
99+
100+ APIConfiguration . shared. environment = APIEnvironment ( rawValue: url. queryParameters ? [ " env " ] ? . lowercased ( ) ?? " prod " ) ?? . production
101+ guard let window = self . window,
102+ let hostingController = window. rootViewController as? UIHostingController < PosmLoginView > else {
103+ debugPrint ( " PosmLoginView not found " )
104+ return
105+ }
106+ let loginView = hostingController. rootView
107+ loginView. viewModel. loginWithRefreshToken ( refreshToken: refreshToken)
108+ }
75109
76110 func scene( _ scene: UIScene , openURLContexts URLContexts: Set < UIOpenURLContext > ) {
77111 print ( " Open contexts called " )
78112 // gaurd and get the first URL
79113 guard let url = URLContexts . first? . url else { return }
80114
81- if url. scheme == " avivscr " {
82- guard !loggedIn else {
83- debugPrint ( " user is already login " )
84- return // user is already login
85- }
86-
87- guard let refreshToken = url. queryParameters ? [ " code " ] else {
88- debugPrint ( " invalid scheme url format " )
89- return // invalid url format
90- }
91-
92- APIConfiguration . shared. environment = APIEnvironment ( rawValue: url. queryParameters ? [ " env " ] ? . lowercased ( ) ?? " prod " ) ?? . production
93- guard let window = self . window,
94- let hostingController = window. rootViewController as? UIHostingController < PosmLoginView > else {
95- debugPrint ( " PosmLoginView not found " )
96- return
97- }
98- let loginView = hostingController. rootView
99- loginView. viewModel. loginWithRefreshToken ( refreshToken: refreshToken)
100- } else {
101- NotificationCenter . default. post ( name: . init( " HandleOAuthRedirect " ) , object: url)
102- }
115+ NotificationCenter . default. post ( name: . init( " HandleOAuthRedirect " ) , object: url)
103116 }
104117}
105118
0 commit comments