Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions skills/firebase-ai-logic-basics/references/ios_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@ let ai = FirebaseAI.firebaseAI()
let model = ai.generativeModel(modelName: "<latest_supported_model>")
```

### 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
)
Comment thread
DjNastor marked this conversation as resolved.

// [AGENT] Replace "<latest_supported_model>" with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt
let model = ai.generativeModel(modelName: "<latest_supported_model>")
```

**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
Expand Down Expand Up @@ -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 "<latest_supported_model>" 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: "<latest_supported_model>")
Comment thread
DjNastor marked this conversation as resolved.

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
Expand Down