Implementing Firebase AI Logic in Your iOS App with Swift
Learn how to integrate Firebase AI Logic into your iOS app with Swift to build dynamic, Gemini AI-powered features.
• 4 min read
Availability
iOS 15
macOS 12
Google Firebase has introduced AI Logic, a new way to build intelligent, dynamic behavior into your mobile apps using natural language prompts and structured logic—without needing to build or fine-tune your own models.
In this guide, we’ll walk through how to integrate Firebase AI Logic into your iOS app using Swift, from setup to implementation.
What Is Firebase AI Logic?
Firebase AI Logic is a managed service that allows you to define, test, and run AI-powered logic blocks using a mix of prompts and structured configuration. Think of it as serverless AI functions that take inputs, apply logic, and return structured outputs.
With AI Logic, you can:
- Create logic using natural language prompts.
- Call this logic from your iOS app using the Firebase AI SDK.
- Use AI-generated decisions to personalize your UI, make recommendations, and more.
Requirements
- Xcode 16.2+
- iOS 15+ or macOS 12+
- Firebase SDK v11.13.0+
- A Firebase-connected app
Firebase Console Setup
- Go to your project in the Firebase Console.
- Under AI, select AI Logic.
- Tap Start, then under Gemini Developer API, tap Start using this API.
- In the dialog that appears, tap Enable. Wait until the API is enabled.
- Click Continue to complete activation.
Enable App Check (Recommended)
To restrict API calls to authorized devices:
- In the AI Logic panel, click Register on App Check.
- In the App Check dashboard, click Start, then Register for the app you want to secure.
- If your app supports only iOS 14+, you can register App Attest only. Otherwise, also register Device Check.
- Complete all required fields and finish registration.
Next:
- In the AI Logic dashboard, go to the APIs section.
- Select Firebase AI Logic APIs, then click Apply to enforce App Check.
Enabling enforcement can take up to 30 minutes to propagate.
Enable App Transport Security Exception
In my case, I also needed to enable App Transport Security Exception.
To do this, go to Project → Signing & Capabilities → Add Capability → App Transport Security Exception.
Inside the capability, tap the + button and add googleapis.com.
Code Implementation
App Check
- Add the Firebase App Check library to your project.
- Create a file named
AppCheckManager.swift
:
import Firebase
import FirebaseAppCheck
final class AppCheckManager: NSObject, AppCheckProviderFactory {
public static let shared = AppCheckManager()
func createProvider(with app: FirebaseApp) -> AppCheckProvider? {
return AppAttestProvider(app: app)
}
public func setProviderFactory() {
#if DEBUG
AppCheck.setAppCheckProviderFactory(AppCheckDebugProviderFactory())
#else
AppCheck.setAppCheckProviderFactory(self)
#endif
}
}
In your AppDelegate
or main app file, before FirebaseApp.configure()
, add:
AppCheckManager.shared.setProviderFactory()
- In your Signing & Capabilities, add the App Attest entitlement.
- In the generated
.entitlements
file, change the App Attest mode fromdevelopment
toproduction
- To enable Firebase debug logging:
-FIRDebugEnabled
Run the app. You should see a log like this:
<Warning> [AppCheckCore][I-GAC004001] App Check debug token: '1E04B27F-15CD-46FA-862B-ED8E67841C9D'.
Copy this token, go to App Check > Apps in the Firebase Console, click the three dots next to your iOS app, choose Manage debug tokens, then add and save the token.
You're done setting up App Check.
Using Firebase AI Logic
Add the FirebaseAI
package:
- Go to General > Frameworks, Libraries, and Embedded Content
- Tap the + button and add
FirebaseAI
Simple Response
To generate a text response from a prompt:
import FirebaseAI
func callAI() async {
do {
let prompt = "Your prompt here..."
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
let model = ai.generativeModel(modelName: "gemini-2.0-flash")
let response = try await model.generateContent(prompt)
if let answer = response.text {
print("FirebaseAI response:", answer)
}
} catch {
print("FirebaseAI error:", error)
}
}
Streamed Response
For a more responsive UI, stream partial results as they’re generated:
func aiStrem() async {
do {
let prompt = "Your prompt here..."
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
let model = ai.generativeModel(modelName: "gemini-2.0-flash")
let response = try model.generateContentStream(prompt)
for try await chunk in response {
if let text = chunk.text {
print("FirebaseAI text:", text)
}
}
} catch {
print("FirebaseAI error:", error)
}
}
Troubleshooting
HTTP 403 Errors
If you see a 403 error:
- Go to the Google Cloud Console
- Select your Firebase project
- Navigate to APIs & Services > Credentials
- Find your iOS key, click it
- Ensure Firebase AI Logic API is enabled under API Restrictions. If not:
If the error persists:
- Temporarily switch to “Don’t restrict key” mode to test.
- You may also try disabling and re-enabling App Check for Firebase AI Logic.
Conclusion
Firebase AI Logic allows iOS developers to integrate intelligent logic blocks into apps without the need to manage ML models or infrastructure. With App Check, you can keep API access secure, and with just a few lines of Swift, you can call or stream AI responses to build truly dynamic user experiences.
Thanks for reading! If you have questions or want a complete sample project, feel free to reach out or leave a comment.
If you have any question about this article, feel free to email me or tweet me @franceleonidev and share your opinion.
Thank you for reading and see you in the next article!
Share this article