AppDelegate.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2020 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import UIKit
  17. import FirebaseCore
  18. import FirebaseAppCheck
  19. @main
  20. class AppDelegate: UIResponder, UIApplicationDelegate {
  21. func application(_ application: UIApplication,
  22. didFinishLaunchingWithOptions launchOptions: [UIApplication
  23. .LaunchOptionsKey: Any]?) -> Bool {
  24. FirebaseApp.configure()
  25. requestDeviceCheckToken()
  26. requestDebugToken()
  27. if #available(iOS 14.0, *) {
  28. requestAppAttestToken()
  29. }
  30. return true
  31. }
  32. // MARK: UISceneSession Lifecycle
  33. func application(_ application: UIApplication,
  34. configurationForConnecting connectingSceneSession: UISceneSession,
  35. options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  36. // Called when a new scene session is being created.
  37. // Use this method to select a configuration to create the new scene with.
  38. return UISceneConfiguration(
  39. name: "Default Configuration",
  40. sessionRole: connectingSceneSession.role
  41. )
  42. }
  43. func application(_ application: UIApplication,
  44. didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  45. // Called when the user discards a scene session.
  46. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
  47. // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
  48. }
  49. // MARK: App Check providers
  50. func requestDeviceCheckToken() {
  51. guard let firebaseApp = FirebaseApp.app() else {
  52. return
  53. }
  54. DeviceCheckProvider(app: firebaseApp)?.getToken { token, error in
  55. if let token = token {
  56. print("DeviceCheck token: \(token.token), expiration date: \(token.expirationDate)")
  57. }
  58. if let error = error {
  59. print("DeviceCheck error: \((error as NSError).userInfo)")
  60. }
  61. }
  62. }
  63. func requestDebugToken() {
  64. guard let firebaseApp = FirebaseApp.app() else {
  65. return
  66. }
  67. if let debugProvider = AppCheckDebugProvider(app: firebaseApp) {
  68. print("Debug token: \(debugProvider.currentDebugToken())")
  69. debugProvider.getToken { token, error in
  70. if let token = token {
  71. print("Debug FAC token: \(token.token), expiration date: \(token.expirationDate)")
  72. }
  73. if let error = error {
  74. print("Debug error: \(error)")
  75. }
  76. }
  77. }
  78. }
  79. @available(iOS 14.0, *)
  80. func requestAppAttestToken() {
  81. guard let firebaseApp = FirebaseApp.app() else {
  82. return
  83. }
  84. guard let appAttestProvider = AppAttestProvider(app: firebaseApp) else {
  85. print("Failed to instantiate AppAttestProvider")
  86. return
  87. }
  88. appAttestProvider.getToken { token, error in
  89. if let token = token {
  90. print("App Attest FAC token: \(token.token), expiration date: \(token.expirationDate)")
  91. }
  92. if let error = error {
  93. print("App Attest error: \(error)")
  94. }
  95. }
  96. }
  97. }