NewThe Flutter Kit — Flutter boilerplate$149$69
Tutorial · SwiftUI + OpenAI · 7 Days to Ship

How to Build an AI Chatbot App with SwiftUI (2026)

Production-grade guide. Streaming responses, markdown rendering, paywall gating, rate limiting. Built to ship in under a week.

Last updated: 2026-05-16 15 min read By Ahmed Gagan, iOS Engineer
Quick Answer

To build an AI chatbot iOS app with SwiftUI in 2026: (1) wire OpenAI streaming via AsyncSequence into an @Observable ChatViewModel, (2) render messages with native SwiftUI AttributedString markdown, (3) gate access via RevenueCat paywall + Supabase per-user rate limits, (4) ship in 3–7 days using The Swift Kit boilerplate. The complete production architecture is below — copy-paste-able Swift code, schema, and paywall config.

Time to ship
3–7 days with boilerplate
Stack
SwiftUI + OpenAI + Supabase + RevenueCat
iOS target
iOS 17+
Inference cost
$0.20–$2.00 per paying user/mo

Skip 4 weeks of setup.

The Swift Kit ships streaming chat UI, OpenAI + Claude + Foundation Models, paywall, rate limiting — all pre-wired.

Get The Swift Kit — $99

The 7 Steps to Ship

Each step takes 4–8 hours with the Swift Kit. From scratch, double that.

  1. 1

    Step 1 — Project setup

    Clone Swift Kit, run setup.sh, pick "AI Chat" preset. The CLI configures: app name, brand colors, OpenAI + Anthropic + Supabase + RevenueCat keys, surface style.

    git clone <swift-kit-repo> MyChatApp
    cd MyChatApp
    ./setup.sh
  2. 2

    Step 2 — Define the chat data model

    Messages have a role, content, timestamp, and optional model metadata. Conform to Identifiable + Hashable for SwiftUI Lists.

    struct ChatMessage: Identifiable, Hashable {
      let id = UUID()
      let role: Role
      var content: String
      let timestamp: Date
    
      enum Role: String { case user, assistant, system }
    }
  3. 3

    Step 3 — Wire OpenAI streaming

    Use AsyncSequence to consume server-sent events. Bind to @Observable so SwiftUI re-renders on every token.

    @Observable @MainActor
    final class ChatViewModel {
      var messages: [ChatMessage] = []
      var isStreaming = false
    
      func send(_ prompt: String) async throws {
        messages.append(.init(role: .user, content: prompt, timestamp: .now))
        var assistant = ChatMessage(role: .assistant, content: "", timestamp: .now)
        messages.append(assistant)
        isStreaming = true
        defer { isStreaming = false }
    
        for try await chunk in openAI.streamCompletion(messages: messages) {
          assistant.content += chunk
          messages[messages.count - 1] = assistant
        }
      }
    }
  4. 4

    Step 4 — Render the chat UI

    Scrollable List with role-styled bubbles. Use AttributedString + .init(markdown:) for inline formatting. Auto-scroll to the latest message with scrollTo(_:anchor:).

    struct ChatView: View {
      @State private var viewModel = ChatViewModel()
      @State private var input = ""
    
      var body: some View {
        VStack {
          ScrollViewReader { proxy in
            List(viewModel.messages) { msg in
              ChatBubble(message: msg)
                .id(msg.id)
            }
            .onChange(of: viewModel.messages.last?.content) {
              if let last = viewModel.messages.last {
                withAnimation { proxy.scrollTo(last.id, anchor: .bottom) }
              }
            }
          }
          ChatComposer(text: $input) {
            Task { try await viewModel.send(input); input = "" }
          }
        }
      }
    }
  5. 5

    Step 5 — Gate with paywall + rate limit

    Check the RevenueCat subscription model before allowing send. For free users, check the Supabase-stored daily quota. Show the paywall when limit hits.

    if !subscription.isPremium && await rateLimiter.exceedsDailyQuota() {
      showPaywall = true
      return
    }
    try await viewModel.send(input)
  6. 6

    Step 6 — Persist message history

    Store conversations in Supabase Postgres with RLS so users only see their own threads. SwiftData (iOS 17+) handles the offline cache.

    // Supabase schema (already in Swift Kit)
    // CREATE TABLE conversations (
    //   id uuid PRIMARY KEY,
    //   user_id uuid REFERENCES auth.users(id),
    //   title text,
    //   created_at timestamptz DEFAULT now()
    // );
    // + RLS: auth.uid() = user_id
  7. 7

    Step 7 — Ship to App Store

    Use the included App Store screenshot templates + description generator. Tag the app correctly (Productivity > Utilities) — NOT "AI" (saturates ranking).

    fastlane release  // or use Xcode Cloud

Frequently Asked Questions

How long does it take to build an AI chatbot app with SwiftUI?
With a SwiftUI boilerplate like The Swift Kit (paywall + auth + AI pre-wired), 3–7 days for an MVP. From scratch: 4–8 weeks. The streaming chat UI alone takes 1–2 weeks to get right (token streaming, markdown rendering, code blocks, auto-scroll, regenerate flow).
What AI model should my chatbot use?
For most consumer chatbots in 2026: GPT-4o-mini ($0.15/$0.60 per 1M tokens) for free tier + Claude Sonnet 4.6 or GPT-5 for premium. Use Apple Foundation Models for offline / privacy-first features. Don't lock yourself into one model — abstract the provider behind a protocol.
How do I stream AI responses in SwiftUI?
Use Swift's AsyncSequence with OpenAI's server-sent events. Bind the streaming string to an @Observable view model and let SwiftUI re-render. Wrap markdown rendering in a debounced parser so tokens flow without thrashing the view tree.
How do I prevent users from abusing free-tier API calls?
Three layers: per-user rate limit via Supabase (e.g., 50 messages/day free), per-message token cap (max_tokens=500), and abuse detection (block prompt injection patterns). Set rate limits on the server (Supabase Edge Functions) — never trust the client.
Should I render markdown in the chat?
Yes. Users expect code blocks, lists, and bold/italic. Use the native SwiftUI AttributedString + Markdown initializer (iOS 15+). For code blocks with syntax highlighting, integrate Splash or a similar library. The Swift Kit ships a markdown-rendering chat bubble.
Does The Swift Kit include AI chatbot scaffolding?
Yes. The Swift Kit includes OpenAI streaming integration, Anthropic Claude integration, Apple Foundation Models scaffold, a chat UI with markdown rendering, message history, paywall gating, rate limiting via Supabase, and onboarding flows tuned for AI apps.

Keep exploring

Ship your iOS app 10× faster

The Swift Kit gives you a production-ready SwiftUI boilerplate — design system, paywall, auth, AI, all pre-wired. $99 one-time.

Get The Swift Kit — $99

One-time purchase · Lifetime updates · 14-day refund