InstallationsAPITests.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 compiler(>=5.5.2) && canImport(_Concurrency)
  46. if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  47. // async/await is a Swift 5.5+ feature available on iOS 15+
  48. Task {
  49. do {
  50. try await Installations.installations().installationID()
  51. } catch {
  52. // ...
  53. }
  54. }
  55. }
  56. #endif // compiler(>=5.5.2) && canImport(_Concurrency)
  57. // Retrieves an installation auth token
  58. Installations.installations().authToken { result, error in
  59. if let _ /* result */ = result {
  60. // ...
  61. } else if let _ /* error */ = error {
  62. // ...
  63. }
  64. }
  65. #if compiler(>=5.5.2) && canImport(_Concurrency)
  66. if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  67. // async/await is a Swift 5.5+ feature available on iOS 15+
  68. Task {
  69. do {
  70. _ = try await Installations.installations().authToken()
  71. } catch {
  72. // ...
  73. }
  74. }
  75. }
  76. #endif // compiler(>=5.5.2) && canImport(_Concurrency)
  77. // Retrieves an installation auth token with forcing refresh parameter
  78. Installations.installations().authTokenForcingRefresh(true) { result, error in
  79. if let _ /* result */ = result {
  80. // ...
  81. } else if let _ /* error */ = error {
  82. // ...
  83. }
  84. }
  85. #if compiler(>=5.5.2) && canImport(_Concurrency)
  86. if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  87. // async/await is a Swift 5.5+ feature available on iOS 15+
  88. Task {
  89. do {
  90. _ = try await Installations.installations().authTokenForcingRefresh(true)
  91. } catch {
  92. // ...
  93. }
  94. }
  95. }
  96. #endif // compiler(>=5.5.2) && canImport(_Concurrency)
  97. // Delete installation data
  98. Installations.installations().delete { error in
  99. if let _ /* error */ = error {
  100. // ...
  101. }
  102. }
  103. #if swift(>=5.5)
  104. if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  105. // async/await is a Swift 5.5+ feature available on iOS 15+
  106. Task {
  107. do {
  108. _ = try await Installations.installations().delete()
  109. } catch let error as NSError
  110. where error.domain == InstallationsErrorDomain && error.code == InstallationsErrorCode
  111. .unknown.rawValue {
  112. // Above is the old way to handle errors.
  113. } catch InstallationsErrorCode.unknown {
  114. // Above is the new way to handle errors.
  115. } catch {
  116. // ...
  117. }
  118. }
  119. }
  120. #endif // swift(>=5.5)
  121. // MARK: - InstallationsAuthTokenResult
  122. Installations.installations().authToken { result, _ in
  123. if let result = result {
  124. _ = result.expirationDate
  125. _ = result.authToken
  126. }
  127. }
  128. // MARK: - InstallationsErrorCode
  129. Installations.installations().authToken { _, error in
  130. if let error = error {
  131. // Old error handling.
  132. switch (error as NSError).code {
  133. case Int(InstallationsErrorCode.unknown.rawValue):
  134. break
  135. case Int(InstallationsErrorCode.keychain.rawValue):
  136. break
  137. case Int(InstallationsErrorCode.serverUnreachable.rawValue):
  138. break
  139. case Int(InstallationsErrorCode.invalidConfiguration.rawValue):
  140. break
  141. default:
  142. break
  143. }
  144. // New error handling.
  145. switch error {
  146. case InstallationsErrorCode.unknown:
  147. break
  148. case InstallationsErrorCode.keychain:
  149. break
  150. case InstallationsErrorCode.serverUnreachable:
  151. break
  152. case InstallationsErrorCode.invalidConfiguration:
  153. break
  154. default:
  155. break
  156. }
  157. }
  158. }
  159. func globalStringSymbols() {
  160. let _: String = InstallationIDDidChangeAppNameKey
  161. let _: String = InstallationsErrorDomain
  162. }
  163. }
  164. }