InstallationsAPITests.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // Copyright 2021 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. // MARK: This file is used to evaluate the experience of using Firebase APIs in Swift.
  17. import Foundation
  18. import FirebaseCore
  19. import FirebaseInstallations
  20. final class InstallationsAPITests {
  21. func usage() {
  22. // MARK: - Installations
  23. // `InstallationIDDidChange` & associated notification keys
  24. _ = NotificationCenter.default
  25. .addObserver(
  26. forName: .InstallationIDDidChange,
  27. object: nil,
  28. queue: .main
  29. ) { notification in
  30. _ = notification.userInfo?[InstallationIDDidChangeAppNameKey]
  31. }
  32. // Retrieving an Installations instance
  33. _ = Installations.installations()
  34. if let app = FirebaseApp.app() {
  35. _ = Installations.installations(app: app)
  36. }
  37. // Create or retrieve an installations ID
  38. Installations.installations().installationID { id, error in
  39. if let _ /* id */ = id {
  40. // ...
  41. } else if let _ /* error */ = error {
  42. // ...
  43. }
  44. }
  45. if #available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  46. // async/await is a Swift Concurrency feature available on iOS 13+ and macOS 10.15+
  47. Task {
  48. do {
  49. try await Installations.installations().installationID()
  50. } catch {
  51. // ...
  52. }
  53. }
  54. }
  55. // Retrieves an installation auth token
  56. Installations.installations().authToken { result, error in
  57. if let _ /* result */ = result {
  58. // ...
  59. } else if let _ /* error */ = error {
  60. // ...
  61. }
  62. }
  63. if #available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  64. // async/await is a Swift Concurrency feature available on iOS 13+ and macOS 10.15+
  65. Task {
  66. do {
  67. _ = try await Installations.installations().authToken()
  68. } catch {
  69. // ...
  70. }
  71. }
  72. }
  73. // Retrieves an installation auth token with forcing refresh parameter
  74. Installations.installations().authTokenForcingRefresh(true) { result, error in
  75. if let _ /* result */ = result {
  76. // ...
  77. } else if let _ /* error */ = error {
  78. // ...
  79. }
  80. }
  81. if #available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  82. // async/await is a Swift Concurrency feature available on iOS 13+ and macOS 10.15+
  83. Task {
  84. do {
  85. _ = try await Installations.installations().authTokenForcingRefresh(true)
  86. } catch {
  87. // ...
  88. }
  89. }
  90. }
  91. // Delete installation data
  92. Installations.installations().delete { error in
  93. if let _ /* error */ = error {
  94. // ...
  95. }
  96. }
  97. #if swift(>=5.5)
  98. if #available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  99. // async/await is a Swift Concurrency feature available on iOS 13+ and macOS 10.15+
  100. Task {
  101. do {
  102. _ = try await Installations.installations().delete()
  103. } catch let error as NSError
  104. where error.domain == InstallationsErrorDomain && error.code == InstallationsErrorCode
  105. .unknown.rawValue {
  106. // Above is the old way to handle errors.
  107. } catch InstallationsErrorCode.unknown {
  108. // Above is the new way to handle errors.
  109. } catch {
  110. // ...
  111. }
  112. }
  113. }
  114. #endif // swift(>=5.5)
  115. // MARK: - InstallationsAuthTokenResult
  116. Installations.installations().authToken { result, _ in
  117. if let result {
  118. _ = result.expirationDate
  119. _ = result.authToken
  120. }
  121. }
  122. // MARK: - InstallationsErrorCode
  123. Installations.installations().authToken { _, error in
  124. if let error {
  125. // Old error handling.
  126. switch (error as NSError).code {
  127. case Int(InstallationsErrorCode.unknown.rawValue):
  128. break
  129. case Int(InstallationsErrorCode.keychain.rawValue):
  130. break
  131. case Int(InstallationsErrorCode.serverUnreachable.rawValue):
  132. break
  133. case Int(InstallationsErrorCode.invalidConfiguration.rawValue):
  134. break
  135. default:
  136. break
  137. }
  138. // New error handling.
  139. switch error {
  140. case InstallationsErrorCode.unknown:
  141. break
  142. case InstallationsErrorCode.keychain:
  143. break
  144. case InstallationsErrorCode.serverUnreachable:
  145. break
  146. case InstallationsErrorCode.invalidConfiguration:
  147. break
  148. default:
  149. break
  150. }
  151. }
  152. }
  153. func globalStringSymbols() {
  154. let _: String = InstallationIDDidChangeAppNameKey
  155. let _: String = InstallationsErrorDomain
  156. }
  157. }
  158. }