InstallationsAPITests.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 swift(>=5.5)
  46. if #available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) {
  47. // async/await is a Swift 5.5+ feature available on iOS 15+
  48. async {
  49. do {
  50. try await Installations.installations().installationID()
  51. } catch {
  52. // ...
  53. }
  54. }
  55. }
  56. #endif // swift(>=5.5)
  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 swift(>=5.5)
  66. if #available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) {
  67. // async/await is a Swift 5.5+ feature available on iOS 15+
  68. async {
  69. do {
  70. _ = try await Installations.installations().authToken()
  71. } catch {
  72. // ...
  73. }
  74. }
  75. }
  76. #endif // swift(>=5.5)
  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 swift(>=5.5)
  86. if #available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) {
  87. // async/await is a Swift 5.5+ feature available on iOS 15+
  88. async {
  89. do {
  90. _ = try await Installations.installations().authTokenForcingRefresh(true)
  91. } catch {
  92. // ...
  93. }
  94. }
  95. }
  96. #endif // swift(>=5.5)
  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 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) {
  105. // async/await is a Swift 5.5+ feature available on iOS 15+
  106. async {
  107. do {
  108. _ = try await Installations.installations().delete()
  109. } catch {
  110. // ...
  111. }
  112. }
  113. }
  114. #endif // swift(>=5.5)
  115. // MARK: - InstallationsAuthTokenResult
  116. Installations.installations().authToken { result, _ in
  117. if let result = result {
  118. _ = result.expirationDate
  119. _ = result.authToken
  120. }
  121. }
  122. // MARK: - InstallationsErrorCode
  123. Installations.installations().authToken { _, error in
  124. if let error = error {
  125. switch (error as NSError).code {
  126. case Int(InstallationsErrorCode.unknown.rawValue):
  127. break
  128. case Int(InstallationsErrorCode.keychain.rawValue):
  129. break
  130. case Int(InstallationsErrorCode.serverUnreachable.rawValue):
  131. break
  132. case Int(InstallationsErrorCode.invalidConfiguration.rawValue):
  133. break
  134. default:
  135. break
  136. }
  137. }
  138. }
  139. }
  140. }