Last week, while debugging a text generation feature in my iOS app, I realized something profound had shifted. Instead of sending user data to remote APIs or wrestling with complex CoreML pipelines, I was working with Apple's new Foundation Models framework—generating text, extracting structured data, and fine-tuning models entirely on-device. This isn't just an incremental update; it's the biggest leap in iOS AI since CoreML first launched.

Photo by Sanket Mishra on Pexels
Table of Contents
- Understanding Apple's Foundation Models Framework
- Setting Up Your Development Environment
- Basic Text Generation with SystemLanguageModel
- Structured Output with @Generable Macro
- Advanced Features: LoRA Adapters and Tool Calling
- Building Your First On-Device AI Feature
- Frequently Asked Questions
Understanding Apple's Foundation Models Framework
With iOS 26, Apple introduced the Foundation Models framework—a Swift-native approach to on-device language model inference. Unlike previous AI implementations that required external dependencies or cloud connectivity, this framework provides direct access to a ~3 billion parameter language model running entirely on your user's device.
Related: Foundation Models Guided Generation with Apple's iOS 26 Framework
The key advantage? Zero API costs, complete privacy, and consistent performance regardless of network conditions. Your AI features work offline, in airplane mode, and without sending sensitive user data anywhere.
Setting Up Your Development Environment
Before diving into on-device AI development, ensure your setup meets the requirements. The Foundation Models framework requires iOS 26+ and runs optimally on devices with A17 Pro+ or M1+ chips.
Also read: Foundation Models Framework Swift Example: On-Device AI
First, import the framework in your Swift files:
import FoundationModels
import SwiftUI
The framework integrates seamlessly with SwiftUI, making it natural to build reactive AI-powered interfaces. You'll notice that unlike traditional ML workflows that require model loading and initialization, SystemLanguageModel.default is immediately available—Apple handles all the heavy lifting behind the scenes.
Basic Text Generation with SystemLanguageModel
The simplest entry point into on-device AI is text generation. Here's how to create a basic AI writing assistant:
struct AIWritingAssistant: View {
@State private var prompt = ""
@State private var generatedText = ""
@State private var isGenerating = false
var body: some View {
VStack(spacing: 20) {
TextField("Enter your writing prompt", text: $prompt)
.textFieldStyle(.roundedBorder)
Button("Generate Text") {
generateText()
}
.disabled(isGenerating || prompt.isEmpty)
if isGenerating {
ProgressView("Generating...")
} else {
Text(generatedText)
.padding()
.background(Color.gray.opacity(0.1))
.cornerRadius(8)
}
}
.padding()
}
private func generateText() {
isGenerating = true
Task {
do {
let response = try await SystemLanguageModel.default.generate(
prompt: prompt,
maxTokens: 200
)
await MainActor.run {
generatedText = response.text
isGenerating = false
}
} catch {
await MainActor.run {
generatedText = "Error: \(error.localizedDescription)"
isGenerating = false
}
}
}
}
}
This example demonstrates streaming text generation with proper async/await handling. The model generates responses in real-time, providing immediate feedback to users.
Structured Output with @Generable Macro
One of the most powerful features in iOS 26's on-device AI toolkit is the @Generable macro. This allows you to define Swift types that the language model can generate directly, eliminating the need for manual JSON parsing or response validation.
@Generable
struct ProductReview {
let title: String
let rating: Int // 1-5 stars
let pros: [String]
let cons: [String]
let summary: String
}
struct ReviewGenerator: View {
@State private var productDescription = ""
@State private var review: ProductReview?
var body: some View {
VStack {
TextField("Describe the product", text: $productDescription)
.textFieldStyle(.roundedBorder)
Button("Generate Review") {
generateReview()
}
if let review = review {
ReviewCard(review: review)
}
}
}
private func generateReview() {
Task {
let prompt = "Generate a realistic product review for: \(productDescription)"
do {
let generatedReview = try await SystemLanguageModel.default.generate(
ProductReview.self,
prompt: prompt
)
await MainActor.run {
self.review = generatedReview
}
} catch {
print("Generation failed: \(error)")
}
}
}
}
The @Generable macro works by analyzing your Swift type at compile time and generating the necessary schema constraints for guided generation. This ensures the language model produces valid, structured output that matches your expected data format.
Advanced Features: LoRA Adapters and Tool Calling
For more sophisticated use cases, iOS 26 supports LoRA (Low-Rank Adaptation) fine-tuning and function calling through the Tool protocol. LoRA adapters allow you to customize the base model's behavior for specific domains without full retraining.
// Define a custom tool for calendar operations
struct CalendarTool: Tool {
let name = "schedule_event"
let description = "Schedules a new calendar event"
struct Parameters: Codable {
let title: String
let date: Date
let duration: TimeInterval
}
func call(with parameters: Parameters) async throws -> String {
// Integrate with EventKit to create actual calendar events
return "Event '\(parameters.title)' scheduled for \(parameters.date)"
}
}
// Load a custom LoRA adapter for domain-specific tasks
let customModel = try await SystemLanguageModel.default.withAdapter(
LoRAAdapter(path: "medical_terminology.lora")
)
These advanced features enable apps to provide highly specialized AI functionality while maintaining the performance and privacy benefits of on-device processing.
Building Your First On-Device AI Feature
Let's put everything together by building a smart note-taking app that automatically categorizes and summarizes user notes:
@Generable
struct NoteAnalysis {
let category: String
let sentiment: String // "positive", "negative", "neutral"
let summary: String
let actionItems: [String]
let tags: [String]
}
struct SmartNoteApp: View {
@State private var noteText = ""
@State private var analysis: NoteAnalysis?
@State private var isAnalyzing = false
var body: some View {
NavigationView {
VStack {
TextEditor(text: $noteText)
.border(Color.gray, width: 1)
.frame(height: 200)
Button("Analyze Note") {
analyzeNote()
}
.disabled(noteText.isEmpty || isAnalyzing)
if isAnalyzing {
ProgressView("Analyzing...")
} else if let analysis = analysis {
AnalysisView(analysis: analysis)
}
}
.padding()
.navigationTitle("Smart Notes")
}
}
private func analyzeNote() {
isAnalyzing = true
Task {
let prompt = """
Analyze this note and provide categorization, sentiment, summary,
action items, and relevant tags:
\(noteText)
"""
do {
let result = try await SystemLanguageModel.default.generate(
NoteAnalysis.self,
prompt: prompt
)
await MainActor.run {
self.analysis = result
self.isAnalyzing = false
}
} catch {
await MainActor.run {
print("Analysis failed: \(error)")
self.isAnalyzing = false
}
}
}
}
}
This example showcases the power of combining structured output with practical AI features. Users can write natural text, and the app automatically extracts meaningful insights without sending data to external services.
The beauty of this approach lies in its simplicity. Traditional AI integration required managing API keys, handling network requests, parsing responses, and dealing with rate limits. With Foundation Models, you write Swift code that feels natural and works reliably.
Frequently Asked Questions
Q: What devices support on-device AI in iOS 26?
The Foundation Models framework requires iOS 26+ and works optimally on devices with A17 Pro or newer chips (iPhone 15 Pro series and later) or M1+ chips on iPads. Older devices may have limited functionality or slower performance.
Q: How do I handle errors when the language model fails to generate expected output?
Wrap your generation calls in do-catch blocks and provide fallback behavior. Common failures include schema validation errors with @Generable types or memory constraints on older devices. Always test error scenarios thoroughly.
Q: Can I use custom training data with the Foundation Models framework?
Directly, no—you can't retrain the base model. However, you can use LoRA adapters to fine-tune behavior for specific domains, or use few-shot prompting techniques to guide the model's responses toward your desired style or format.
Q: How does on-device AI performance compare to cloud-based solutions?
On-device AI trades some capability for privacy and reliability. The ~3B parameter model is smaller than cloud models like GPT-4, but it's instant, works offline, and costs nothing per request. For most iOS app use cases, the performance is more than adequate.
Apple's Foundation Models framework represents a fundamental shift in how we think about AI integration in mobile apps. Instead of treating AI as an external service, it becomes a native capability—as accessible as Core Data or AVFoundation. As developers, we're just beginning to explore what's possible when every iPhone becomes an AI-capable device.
The framework's emphasis on privacy, performance, and developer experience makes it clear that 2026 is the year on-device AI becomes mainstream in iOS development. Whether you're building productivity apps, creative tools, or data analysis features, the Foundation Models framework provides the tools to make your apps more intelligent without compromising user privacy.
Need a server? Get $200 free credits on DigitalOcean to deploy your AI apps.
Resources I Recommend
If you're serious about iOS AI development, this collection of Swift programming books helped me understand the fundamentals of building robust iOS applications that can effectively leverage Apple's new AI capabilities.
You Might Also Like
- Foundation Models Guided Generation with Apple's iOS 26 Framework
- Foundation Models Framework Swift Example: On-Device AI
- On-Device AI iOS 26: Build Your First Foundation Model App
📘 Go Deeper: AI-Powered iOS Apps: CoreML to Claude
200+ pages covering CoreML, Vision, NLP, Create ML, cloud AI integration, and a complete capstone app — with 50+ production-ready code examples.
Also check out: *Building AI Agents***
Enjoyed this article?
I write daily about iOS development, AI, and modern tech — practical tips you can use right away.
- Follow me on Dev.to for daily articles
- Follow me on Hashnode for in-depth tutorials
- Follow me on Medium for more stories
- Connect on Twitter/X for quick tips
If this helped you, drop a like and share it with a fellow developer!
Top comments (0)