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.

Francesco Leoni

• 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

  1. Go to your project in the Firebase Console.
  2. Under AI, select AI Logic.
  3. Tap Start, then under Gemini Developer API, tap Start using this API.
  4. In the dialog that appears, tap Enable. Wait until the API is enabled.
  5. Click Continue to complete activation.

Enable App Check (Recommended)

To restrict API calls to authorized devices:

  1. In the AI Logic panel, click Register on App Check.
  2. In the App Check dashboard, click Start, then Register for the app you want to secure.
  3. If your app supports only iOS 14+, you can register App Attest only. Otherwise, also register Device Check.
  4. Complete all required fields and finish registration.

Next:

  1. In the AI Logic dashboard, go to the APIs section.
  2. 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

  1. Add the Firebase App Check library to your project.
  2. 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()

  1. In your Signing & Capabilities, add the App Attest entitlement.
  2. In the generated .entitlements file, change the App Attest mode from development to production
  3. To enable Firebase debug logging:
  • Go to Product > Scheme > Edit Scheme…
  • Select Run > Arguments
  • Add a new argument: -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:

    1. Go to the Google Cloud Console
    2. Select your Firebase project
    3. Navigate to APIs & Services > Credentials
    4. Find your iOS key, click it
    5. Ensure Firebase AI Logic API is enabled under API Restrictions. If not:
  • Select it
  • Click Save
  • Wait up to 30 minutes before retrying
  • 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

    Related articles


    Fix “Cycle Inside“ Error in Xcode

    See how to fix the “Cycle Inside“ error in Xcode. This error happens because there is an issue with the order of scripts.

    • 1 min read

    Q&AXcodeErrors

    Force Dark Mode to View in SwiftUI

    In this article we are going to learn how to apply a specific colour scheme to a View regardless of the system dark mode.

    • 1 min read

    SwiftUI

    SwiftData the Successor of CoreData Explained with SwiftUI

    Discover the SwiftData framework built on top of CoreData. Save and fetch data locally. Available for Xcode 15, Swift and SwiftUI.

    • 5 min read

    SwiftUICoreData