SceneDelegate.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // Implementing this delegate method is needed when swizzling is disabled.
  43. // Without it, reCAPTCHA's login view controller will not dismiss.
  44. func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
  45. for urlContext in URLContexts {
  46. let url = urlContext.url
  47. _ = Auth.auth().canHandle(url)
  48. }
  49. // URL not auth related; it should be handled separately.
  50. }
  51. func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
  52. if let incomingURL = userActivity.webpageURL {
  53. handleIncomingDynamicLink(incomingURL)
  54. }
  55. }
  56. // MARK: - Firebase 🔥
  57. private func handleIncomingDynamicLink(_ incomingURL: URL) {
  58. let link = incomingURL.absoluteString
  59. if AppManager.shared.auth().isSignIn(withEmailLink: link) {
  60. // Save the link as it will be used in the next step to complete login
  61. UserDefaults.standard.set(link, forKey: "Link")
  62. // Post a notification to the PasswordlessViewController to resume authentication
  63. NotificationCenter.default
  64. .post(Notification(name: Notification.Name("PasswordlessEmailNotificationSuccess")))
  65. }
  66. }
  67. // MARK: - Private Helpers
  68. private func configureControllers() {
  69. authNavController.configureTabBar(
  70. title: "Authentication",
  71. systemImageName: "person.crop.circle.fill.badge.plus"
  72. )
  73. userNavController.configureTabBar(title: "Current User", systemImageName: "person.fill")
  74. tabBarController.viewControllers = [authNavController, userNavController]
  75. }
  76. }