VertexAISampleApp.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2023 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 FirebaseAppCheck
  15. import FirebaseCore
  16. import SwiftUI
  17. class AppDelegate: NSObject, UIApplicationDelegate {
  18. func application(_ application: UIApplication,
  19. didFinishLaunchingWithOptions launchOptions: [UIApplication
  20. .LaunchOptionsKey: Any]? = nil) -> Bool {
  21. // Recommendation: Protect your Vertex AI API resources from abuse by preventing unauthorized
  22. // clients using App Check; see https://firebase.google.com/docs/app-check#get_started.
  23. AppCheck.setAppCheckProviderFactory(AppCheckNotConfiguredFactory())
  24. FirebaseApp.configure()
  25. if let firebaseApp = FirebaseApp.app(), firebaseApp.options.projectID == "mockproject-1234" {
  26. guard let bundleID = Bundle.main.bundleIdentifier else { fatalError() }
  27. fatalError("""
  28. You must create and/or download a valid `GoogleService-Info.plist` file for \(bundleID) from \
  29. https://console.firebase.google.com to run this sample. Replace the existing \
  30. `GoogleService-Info.plist` file in the `FirebaseVertexAI/Sample` directory with this new file.
  31. """)
  32. }
  33. return true
  34. }
  35. }
  36. @main
  37. struct VertexAISampleApp: App {
  38. @UIApplicationDelegateAdaptor var appDelegate: AppDelegate
  39. var body: some Scene {
  40. WindowGroup {
  41. ContentView()
  42. }
  43. }
  44. }
  45. /// Placeholder App Check provider factory that returns a simple ``AppCheckNotConfigured`` error.
  46. private class AppCheckNotConfiguredFactory: NSObject, AppCheckProviderFactory {
  47. private class AppCheckNotConfiguredProvider: NSObject, AppCheckProvider {
  48. func getToken() async throws -> AppCheckToken {
  49. throw AppCheckNotConfigured()
  50. }
  51. }
  52. func createProvider(with app: FirebaseApp) -> (any AppCheckProvider)? {
  53. return AppCheckNotConfiguredProvider()
  54. }
  55. }
  56. /// Error indicating that App Check is not configured in the sample app.
  57. struct AppCheckNotConfigured: Error {}