๐ง๐ฟ๐๐ถ๐ป๐ด ๐๐ฝ๐ฝ๐น๐ฒ ๐๐ผ๐๐ป๐ฑ๐ฎ๐๐ถ๐ผ๐ป ๐ ๐ผ๐ฑ๐ฒ๐น๐ ๐๐ถ๐๐ต ๐ข๐ป-๐๐ฒ๐๐ถ๐ฐ๐ฒ ๐๐
Apple now gives iOS developers a native way to use generative AI. The Foundation Models framework lets you run AI features directly on a device using Apple Intelligence.
I built a small SwiftUI experiment to test this. It is an ambient sound mixer. You type a prompt, and the AI creates a list of sounds and volume levels to build a soundscape.
To build this, you need:
- Xcode 27 with the Foundation Models framework.
- A device that supports Apple Intelligence.
- Apple Intelligence enabled on that device.
The best part of this framework is guided generation.
In most AI workflows, you ask a model for JSON. You then hope the formatting is correct. You spend time parsing strings and handling errors.
With guided generation, you describe a Swift type. The framework ensures the model output matches your type exactly.
I used the @Generable macro to define the output:
- I defined an AmbientMix struct.
- I added constraints using @Guide.
- The model must return between 1 and 4 sounds.
This removes the need for JSONDecoder or regex. The model returns a real Swift value.
Steps to implement this:
Check availability Do not assume the model is ready. Users might have unsupported devices or disabled settings. Always check if the model is available before running tasks.
Create a session Use a LanguageModelSession. Give it developer instructions. Tell the model to act as a sound assistant. Instruct it to use only your local files and not invent new names.
Generate the value Ask the model to respond by generating your specific Swift type.
Validate the result The model can suggest things, but your app must decide what is allowed. Check if the file names exist in your library. Clamp the volume levels to your allowed range.
Connect to UI Use SwiftUI to trigger the generation inside a Task. Show a loading state and handle errors clearly.
Foundation Models is not for every task. If you need heavy reasoning or large context, use a server-side model.
But for privacy-friendly, focused features with structured outputs, this framework is excellent. It provides a clean developer experience for real iOS apps.
Source: https://developer.apple.com/documentation/FoundationModels Source: https://dev.to/mashiruwu/trying-apple-foundation-models-with-on-device-ai-37f3
Optional learning community: https://t.me/GyaanSetuAi