It is a free pattern chapter from Apple Basis Fashions by Invoice Morefield. Learn it, construct with it, and for those who’d like the remaining, there’s a hyperlink on the finish.
Machine studying and different synthetic intelligence techniques have gone from curiosities to helpful instruments in the previous couple of years. Whereas their use circumstances are sometimes overhyped, the truth that they will present clear worth to your app in the appropriate conditions is obvious. Now you can full duties on a tool that matches in your hand that had been beforehand tough or not possible, even on enterprise gear. Just a few of those applied sciences have garnered the hype and controversy as Massive Language Fashions (LLMs). A conventional LLM requires an enormous quantity of computational energy, reminiscence, and assets to run. Properly-funded startups had been the one ones capable of prepare and run these fashions and deploy big quantities of reminiscence, storage, and computing energy.
To deal with these excessive system necessities, many programmers have explored deploying native fashions. These fashions are optimized and simplified to run on the units and gear of on a regular basis customers. Beginning with iOS 26, iPadOS 26, macOS 26, and different model 26 working techniques, Apple is offering its personal native mannequin, optimized to be used in apps, referred to as Apple Basis Fashions. Apple Basis Fashions are Apple’s on-device AI fashions, designed to guard privateness whereas serving to with duties like writing textual content, summarizing data, and organizing knowledge on supported units. As a result of all knowledge stays on the gadget, you don’t want an web connection, there may be much less latency, and also you keep away from the privateness dangers that come when sending knowledge to third-party companies. This ebook will discover the usage of Apple Basis Fashions in your apps.
What’s Apple Basis Fashions?
It’s value beginning with essentially the most primary query: What’s Apple Basis Fashions Framework? The quick reply is that it’s a massive language mannequin (LLM) that Apple has optimized to run regionally on end-user units, reminiscent of laptops, desktops, and cellular units. Conventional LLMs function in knowledge facilities outfitted with high-powered GPUs, which want loads of reminiscence and energy. Bringing that performance to an end-user gadget requires important modifications to the mannequin. In Apple’s case, the 2 most essential modifications to supply Basis Fashions are lowering the variety of parameters and quantizing the values that kind the mannequin. You’ll study extra about how they did that later.
On this chapter, you’ll develop a chat-style app that interacts with Basis Fashions to discover the probabilities and limitations of this framework. A chat app isn’t an ideal use case for Basis Fashions as a result of small measurement of the on-device mannequin, however it offers a well-understood app to discover integrating Apple Basis Fashions right into a SwiftUI app. The fast suggestions may even make it simpler to discover the mannequin’s use and limitations.
Utilizing Basis Fashions
Open the starter app in Xcode 26 or later. Run the app, and you will note the starter implements a easy chat-style interface. The textbox on the backside of the view offers the person a spot to enter textual content and “ship” it. Proper now, the chat will echo any textual content entered.
Word: When operating apps that use Basis Fashions within the simulator, the simulator makes use of the underlying gadget’s Apple Intelligence. Meaning you need to run on macOS not less than the identical model as Xcode and the simulator you’re utilizing. This contains beta variations. You can not run a simulator with iOS 2.64 beta on macOS 26.3 and simulate Basis Fashions. If the variations don’t line up, utilizing Basis Fashions will return an error.
You’ll now replace this app to make use of Apple Basis Fashions. The textual content you ship to the mannequin is a immediate. The mannequin will then present a response, which the app will show to the person.
Earlier than you attempt to use Basis Fashions, be sure that it’s accessible on the gadget. The gadget should help Apple Intelligence, and the person should flip it on for his or her gadget. If both of these isn’t carried out, then your app might want to disable or work round mannequin options. So your first step is to make sure that Basis Fashions can be found. At the moment, the app solely exhibits the ChatView, however you’ll as a substitute show a message if the gadget doesn’t help Basis Fashions.
Open ContentView.swift and add the next code because the final import:
import FoundationModels
To make use of Basis Fashions in a category or view, you have to first embody the framework. Now add the next property to the view:
non-public let mannequin = SystemLanguageModel.default
SystemLanguageModel refers back to the on-device textual content basis mannequin. The default property accesses the bottom model of the mannequin. You’ll use this to confirm the standing of Basis Fashions. Exchange the physique of the view with:
swap mannequin.availability {
case .accessible:
ChatView()
case .unavailable(let cause):
ModelUnavailableView(cause: cause)
}
ModelUnavailableView doesn’t exist but, however you’ll maintain that subsequent. The mannequin.availability property displays the state of Basis Fashions on the gadget. It should both be accessible, which means your app can entry Basis Fashions, or unavailable, which additionally offers a cause explaining why Basis Fashions isn’t accessible. When accessible, you present the prevailing ChatView as earlier than. However when Basis Fashions is unavailable, you’ll show ModelUnavailableView to tell the person.
Create a brand new SwiftUI view named ModelUnavailableView within the Chat Views folder. Add the next to the imports on the prime of the brand new view:
import FoundationModels
Once more, you want this import in any code that interacts with Basis Fashions. Subsequent, add the next cause property to go into the view:
var cause: SystemLanguageModel.Availability.UnavailableReason
This property holds an enumerable that explains why the mannequin is unavailable. Your app can use this to show an acceptable message. Change the physique of the view to:
Picture(systemName: "apple.intelligence")
.font(.largeTitle)
swap cause {
case .deviceNotEligible:
Textual content("Apple Intelligence isn't accessible on this gadget.")
case .appleIntelligenceNotEnabled:
Textual content("Apple Intelligence is offered, however not enabled on this gadget.")
case .modelNotReady:
Textual content("The mannequin is not prepared. This is actually because it's nonetheless downloading.")
@unknown default:
Textual content("An unknown error prevents Apple Intelligence from working.")
}
This can show the Apple Intelligence SF Image together with a user-friendly textual content message for the most typical causes. You additionally present a generic error for different circumstances, utilizing @unknown default to future-proof in opposition to new enum values. This can put together your app for any future modifications to the framework. Now replace the preview to:
ModelUnavailableView(cause: .appleIntelligenceNotEnabled)
This offers a cause for the preview. Viewing the Canvas will now present this default view.
Now, run your app. In case your gadget meets the necessities described earlier, you need to nonetheless have the ability to see the chat app. In case your gadget doesn’t help Apple Intelligence, you will note the informational view to that impact, together with the explanation. After all, when testing your app, you’ll want to make sure the person both will get a profitable fallback or an acceptable informational message for these error states. To check this, you should use the scheme possibility in XCode.
Word: If that is the primary time you’re utilizing Apple Basis Fashions, it might take 15 to 60 minutes for the mannequin to obtain on the gadget. Be sure that the gadget has a connection to the web whereas it’s downloading.

