diff --git a/skills/firebase-ai-logic-basics/references/ios_setup.md b/skills/firebase-ai-logic-basics/references/ios_setup.md index a2052e24..2ebc1262 100644 --- a/skills/firebase-ai-logic-basics/references/ios_setup.md +++ b/skills/firebase-ai-logic-basics/references/ios_setup.md @@ -14,6 +14,32 @@ let ai = FirebaseAI.firebaseAI() let model = ai.generativeModel(modelName: "") ``` +### Using Limited-Use App Check Tokens + +For enhanced security and cost control, you can enable limited-use tokens during instantiation. This restricts each token to a single API call, reducing risk if a token is compromised: + +```swift +import FirebaseAILogic + +// Initialize with limited-use tokens enabled +let ai = FirebaseAI.firebaseAI( + backend: .geminiDeveloperAPI(), + useLimitedUseAppCheckTokens: true +) + +// [AGENT] Replace "" with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt +let model = ai.generativeModel(modelName: "") +``` + +**Benefits:** +- Each App Check token is valid for only one API request +- Reduces attack surface if a token is intercepted +- Ideal for production applications with strict security requirements + +**Prerequisites:** +- App Check must be configured in your Firebase project +- Ensure your app's attestation provider is set up (DeviceCheck for iOS) + ## 2. SwiftUI Integration (Best Practices) Use the `@Observable` pattern to manage AI state and provide a smooth UX with @@ -81,6 +107,43 @@ struct AIView: View { } ``` +### With Limited-Use Tokens + +For enhanced security in production, use limited-use App Check tokens: + +```swift +import SwiftUI +import FirebaseAILogic + +@MainActor +@Observable +final class SecureAIViewModel { + // Initialize with limited-use tokens for enhanced security + // [AGENT] Replace "" with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt + private lazy var model = FirebaseAI.firebaseAI( + backend: .geminiDeveloperAPI(), + useLimitedUseAppCheckTokens: true + ).generativeModel(modelName: "") + + var responseText: String = "" + var isFetching: Bool = false + var errorMessage: String? + + func generate(prompt: String) async { + isFetching = true + errorMessage = nil + defer { isFetching = false } + + do { + let response = try await model.generateContent(prompt) + self.responseText = response.text ?? "No response" + } catch { + self.errorMessage = error.localizedDescription + } + } +} +``` + ## 3. Safety Settings You can configure safety thresholds to prevent the model from generating harmful