At WWDC 2025, Apple introduced the Foundation Models framework, a new way for any iOS developer to integrate language models directly into applications using Swift programming. This framework enables intelligent experiences without relying on external services while preserving user privacy.
In this tutorial, you will learn how to use Foundation Models with Swift, SwiftUI, and Xcode to build applications compatible with iOS, macOS, and watchOS.
What Is Foundation Models?
Foundation Models is a native Apple framework that provides access to on-device language models. For Swift developers, this means artificial intelligence is integrated just like any other system framework.
Prerequisites
- Updated Xcode
- A device compatible with Apple Intelligence
- Basic knowledge of Swift and SwiftUI
- Familiarity with asynchronous programming
Importing Foundation Models into Your Project
To start using Foundation Models, simply import the framework into your Swift file.
import FoundationModels
First Example: Text Generation
Let’s create a basic function that generates text from a prompt using the system language model.
import FoundationModels
func generarTexto(prompt: String) async throws -> String {
let modelo = LanguageModel()
let configuracion = LanguageModelConfiguration()
let sesion = try modelo.session(configuration: configuracion)
let resultado = try await sesion.generate(prompt: prompt)
return resultado.text
}
SwiftUI Integration
Now let’s integrate this functionality into a view built with SwiftUI.
import SwiftUI
struct ContentView: View {
@State private var textoUsuario = ""
@State private var respuesta = ""
var body: some View {
VStack(spacing: 16) {
TextField("Escribe una pregunta", text: $textoUsuario)
.textFieldStyle(.roundedBorder)
Button("Generar respuesta") {
Task {
await generar()
}
}
ScrollView {
Text(respuesta)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
.padding()
}
func generar() async {
do {
respuesta = try await generarTexto(prompt: textoUsuario)
} catch {
respuesta = "Error al generar la respuesta"
}
}
}
Guided Generation with Swift Types
Foundation Models allows you to generate structured data directly into Swift types, improving both safety and reliability.
import FoundationModels
struct Recomendacion: Generable {
let titulo: String
let descripcion: String
let duracion: Int
}
Now we ask the model to generate an instance of this type.
let prompt = "Crea una recomendación de actividad para un fin de semana en Madrid"
let resultado = try await sesion.generate(prompt: prompt, as: Recomendacion.self)
Streaming Results
To improve the user experience, you can display the model’s response as it is being generated.
let stream = try await sesion.stream(prompt: "Resume este texto en pocas líneas")
for await fragmento in stream {
respuestaParcial += fragmento
}
Session Context Management
Sessions allow you to maintain context across multiple interactions.
let sesion = try modelo.session(configuration: configuracion)
try await sesion.addContext("El usuario está planificando un viaje")
Complete Example: Travel Assistant
This example shows a ViewModel that uses Foundation Models to generate a travel plan.
@MainActor
class TravelViewModel: ObservableObject {
@Published var resultado = ""
func generarPlan(destino: String) async {
let prompt = "Genera un plan de viaje de tres días en \(destino)"
do {
resultado = try await generarTexto(prompt: prompt)
} catch {
resultado = "No se pudo generar el plan"
}
}
}
Associated SwiftUI view:
struct TravelView: View {
@StateObject private var viewModel = TravelViewModel()
@State private var destino = ""
var body: some View {
VStack {
TextField("Destino", text: $destino)
.textFieldStyle(.roundedBorder)
Button("Generar plan") {
Task {
await viewModel.generarPlan(destino: destino)
}
}
ScrollView {
Text(viewModel.resultado)
}
}
.padding()
}
}
Conclusion
Foundation Models represents a major step forward for any iOS developer working with Swift, SwiftUI, and Xcode. It enables artificial intelligence to be integrated directly into applications for iOS, macOS, and watchOS while maintaining privacy, performance, and a clean API.
Mastering this framework is essential for the future of Swift programming within the Apple ecosystem.
If you have any questions about this article, please contact me and I will be happy to help you 🙂. You can contact me on my X profile or on my Instagram profile.