SceneDelegate.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 FirebaseDynamicLinks
  16. import UIKit
  17. class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  18. var window: UIWindow?
  19. lazy var authNavController: UINavigationController = {
  20. let navController = UINavigationController(rootViewController: AuthViewController())
  21. navController.view.backgroundColor = .systemBackground
  22. return navController
  23. }()
  24. lazy var userNavController: UINavigationController = {
  25. let navController = UINavigationController(rootViewController: UserViewController())
  26. navController.view.backgroundColor = .systemBackground
  27. return navController
  28. }()
  29. lazy var tabBarController: UITabBarController = {
  30. let tabBarController = UITabBarController()
  31. tabBarController.delegate = tabBarController
  32. tabBarController.view.backgroundColor = .systemBackground
  33. return tabBarController
  34. }()
  35. func scene(_ scene: UIScene, willConnectTo session: UISceneSession,
  36. options connectionOptions: UIScene.ConnectionOptions) {
  37. guard let windowScene = (scene as? UIWindowScene) else { return }
  38. configureControllers()
  39. window = UIWindow(windowScene: windowScene)
  40. window?.rootViewController = tabBarController
  41. window?.makeKeyAndVisible()
  42. }
  43. func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
  44. if let incomingURL = userActivity.webpageURL {
  45. handleIncomingDynamicLink(incomingURL)
  46. }
  47. }
  48. // MARK: - Firebase 🔥
  49. private func handleIncomingDynamicLink(_ incomingURL: URL) {
  50. DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { dynamicLink, error in
  51. guard error == nil else {
  52. return print("ⓧ Error in \(#function): \(error!.localizedDescription)")
  53. }
  54. guard let link = dynamicLink?.url?.absoluteString else { return }
  55. if AppManager.shared.auth().isSignIn(withEmailLink: link) {
  56. // Save the link as it will be used in the next step to complete login
  57. UserDefaults.standard.set(link, forKey: "Link")
  58. // Post a notification to the PasswordlessViewController to resume authentication
  59. NotificationCenter.default
  60. .post(Notification(name: Notification.Name("PasswordlessEmailNotificationSuccess")))
  61. }
  62. }
  63. }
  64. // MARK: - Private Helpers
  65. private func configureControllers() {
  66. authNavController.configureTabBar(
  67. title: "Authentication",
  68. systemImageName: "person.crop.circle.fill.badge.plus"
  69. )
  70. userNavController.configureTabBar(title: "Current User", systemImageName: "person.fill")
  71. tabBarController.viewControllers = [authNavController, userNavController]
  72. }
  73. }