Introduction to Apple Foundation Models

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Open the starter app in Xcode 26. You will find a simple starter app that implements a simple chat-style interface. A textbox at the bottom of the view allows the user to enter text and “send” it. Any text entered will be echoed back to the user after a short, artificial delay.

Starter App Echos text back to user.
Starter App Echos text back to user.

You’ll now update this app to use Apple Foundation Models. This will send the entered text to the model as a prompt. The model will then provide a response, which you will display to the user. While a chat app isn’t a good use case for Foundation Models, it facilitates easy experimentation with the model and demonstrates how to integrate it into a SwiftUI app.

Before you try to use Foundation Models, ensure that it is available on the device. The device must support Apple Intelligence, and the user needs to turn on Apple Intelligence on their device. Our first step is to ensure that Foundation Models are available.

To use Foundation Models in your app, you must first include the framework. Open ContentView.swift and add the following code as the last import:

import FoundationModels

Now add the following property to the view:

private let model = SystemLanguageModel.default

SystemLanguageModel refers to the on-device text foundation model. The default property accesses the base version of the model. You will use this to verify the status of Foundation Models. Replace the body of the view with:

switch model.availability {
case .available:
  ChatView()
case .unavailable(let reason):
  ModelUnavailableView(reason: reason)
}

The model.availability property provides the state of Foundation Models on the device. It will either be available, meaning Foundation Models are available or unavailable, which also provides a reason to explain why Foundation Models is not accessible. This first view will then show the existing ChatView when the FM are available and a ModelUnavailableView when it is not.

Create a new SwiftUI view named ModelUnavailableView in the Chat Views folder. Add the following to the imports at the top of the new view:

import FoundationModels

Again, this import is needed in any code that interacts with Foundation Models. Next, add the following reason property that will be passed into the view:

var reason: SystemLanguageModel.Availability.UnavailableReason

This property holds an enumerable that provides the reason the model is unavailable. Your app can use this to display an appropriate message. Change the contents of the view to:


switch reason {
Image(systemName: "apple.intelligence")
  .font(.largeTitle)
case .deviceNotEligible:
  Text("Apple Intelligence is not available on this device.")
case .appleIntelligenceNotEnabled:
  Text("Apple Intelligence is available, but not enabled on this device.")
case .modelNotReady:
  Text("The model isn't ready. This is usually because it is still downloading.")
@unknown default:
  Text("An unknown error prevents Apple Intelligence from working.")
}

This will display the Apple Intelligence SF Symbol along with a text message for the most common reasons. You also provide a generic error for any other cases, using @unknown default to future-proof against new enum values. Now update the preview to:

ModelUnavailableView(reason: .appleIntelligenceNotEnabled)

This provides a reason for the preview. Now, build and run your app. If your device meets the requirements described earlier, you should still be able to see the chat app. If your device doesn’t support Apple Intelligence, you will see the informational view to that effect.

View Shown When Foundation Models Not Available
View Shown When Foundation Models Not Available

In the next section, you’ll see how to use Apple Foundation Models from an app.

See forum comments
Download course materials from Github
Previous: Introduction Next: Using Foundation Models