SceneDelegate.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import FirebaseAuth
  15. import UIKit
  16. class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  17. var window: UIWindow?
  18. lazy var authNavController: UINavigationController = {
  19. let navController = UINavigationController(rootViewController: AuthViewController())
  20. navController.view.backgroundColor = .systemBackground
  21. return navController
  22. }()
  23. lazy var userNavController: UINavigationController = {
  24. let navController = UINavigationController(rootViewController: UserViewController())
  25. navController.view.backgroundColor = .systemBackground
  26. return navController
  27. }()
  28. lazy var tabBarController: UITabBarController = {
  29. let tabBarController = UITabBarController()
  30. tabBarController.delegate = tabBarController
  31. tabBarController.view.backgroundColor = .systemBackground
  32. return tabBarController
  33. }()
  34. func scene(_ scene: UIScene, willConnectTo session: UISceneSession,
  35. options connectionOptions: UIScene.ConnectionOptions) {
  36. guard let windowScene = (scene as? UIWindowScene) else { return }
  37. configureControllers()
  38. window = UIWindow(windowScene: windowScene)
  39. window?.rootViewController = tabBarController
  40. window?.makeKeyAndVisible()
  41. }
  42. func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
  43. if let incomingURL = userActivity.webpageURL {
  44. handleIncomingDynamicLink(incomingURL)
  45. }
  46. }
  47. // MARK: - Firebase 🔥
  48. private func handleIncomingDynamicLink(_ incomingURL: URL) {
  49. let link = incomingURL.absoluteString
  50. if AppManager.shared.auth().isSignIn(withEmailLink: link) {
  51. // Save the link as it will be used in the next step to complete login
  52. UserDefaults.standard.set(link, forKey: "Link")
  53. // Post a notification to the PasswordlessViewController to resume authentication
  54. NotificationCenter.default
  55. .post(Notification(name: Notification.Name("PasswordlessEmailNotificationSuccess")))
  56. }
  57. }
  58. // MARK: - Private Helpers
  59. private func configureControllers() {
  60. authNavController.configureTabBar(
  61. title: "Authentication",
  62. systemImageName: "person.crop.circle.fill.badge.plus"
  63. )
  64. userNavController.configureTabBar(title: "Current User", systemImageName: "person.fill")
  65. tabBarController.viewControllers = [authNavController, userNavController]
  66. }
  67. }