A small SwiftUI demo app that moderates user-entered team names entirely on-device using Apple's Foundation Models framework.
You add friendly five-a-side football teams to a list. Before a name is accepted, an on-device language model checks it for profanity, slurs, sexual content, or hateful language — no network calls, nothing leaves the device.
ContentView— the team list UI. Adding a name runs it through the moderator and either appends the team or shows a rejection reason.TeamNameModerator— wraps aLanguageModelSessionand performs moderation. It uses two layers of protection:- Apple's built-in safety guardrails (
guardrailViolation) catch blatant content. - A guided-generation classifier judges subtler cases.
- Apple's built-in safety guardrails (
TeamNameVerdict— a@Generablestruct used for guided generation. Fields are ordered so the model writes itsassessmentbefore committing to theisInappropriateboolean, which improves accuracy.Team— a simpleIdentifiablemodel.
- Fails open. If the model isn't available on the device, or a check can't complete, names are allowed rather than blocking the user.
- Prewarming. The session is warmed up when the text field gains focus to reduce first-check latency.
- Prompt safety. Moderation instructions deliberately avoid quoting example profanity, since the guardrail scans the entire prompt.
- Xcode 16 or later
- A device/OS version that supports the Foundation Models framework with Apple Intelligence enabled
- Swift 5.0+ language mode
Note: The Swift module is named
TeamNamesApp(set viaPRODUCT_MODULE_NAME) so it doesn't collide with Apple'sFoundationModelsframework, while the app product is still named FoundationModels.
- Open
FoundationModels.xcodeprojin Xcode. - Select a supported device (or simulator with Apple Intelligence available).
- Build and run.
On unsupported devices the app still runs — moderation simply allows all names.