Why FoundationModels?
The framework offers three key advantages:
- Complete Privacy: All processing happens on-device
- Offline Capability: Works without internet once loaded
- Zero App Bloat: Model is embedded in the OS, not your app
Essential Development Techniques
1. Xcode Playgrounds for Prompt Engineering
Instead of constantly rebuilding apps to test prompts, use Xcode’s updated Playground feature. Import Playgrounds in any project file, use #Playground
to test FoundationModels code directly, and get live feedback as you refine prompts.
2. Structured Output with Guided Generation
Use the @Generable
annotation to get structured data instead of parsing text. Create data structures like Itinerary with nested types, and the model automatically generates matching Swift objects.
The @Guide
macro provides fine control – add descriptions, constrain values, set count requirements, or apply multiple guides to properties.
3. Tool Calling for External Data
Create tools that conform to the Tool protocol to let the model fetch external information autonomously. The demo showed a MapKit integration that finds points of interest, with the model intelligently choosing appropriate categories for each location.
4. Handle Model Availability
The on-device model isn’t always available. Handle three scenarios:
- Device Not Eligible: Hide AI features entirely
- Not Enabled: Guide users to enable Apple Intelligence
- Not Ready: Ask users to try again later
Use Xcode’s Foundation Models Availability override to test these scenarios.
5. Streaming for Better UX
Stream partial results using PartiallyGenerated
versions of your data structures. Users can start reading content while the model generates the rest, creating a more responsive experience.
6. Performance Optimization
The session highlighted two key optimizations using Apple’s FoundationModels Instrument:
Prewarming: Load the model when users show intent (like selecting a landmark) rather than waiting for generation requests.
Schema Optimization: Set includeSchemaInPrompt
to false when the model already understands the format, reducing tokens and improving performance.
Key Takeaways
- Use Playgrounds for efficient prompt iteration
- Structure your output with guided generation
- Plan for availability – handle different device states gracefully
- Stream responses for better perceived performance
- Optimize strategically with prewarming and schema settings
- Profile performance using the FoundationModels Instrument
Testing Tips
- Use simulators for functionality testing when your Mac has Apple Intelligence
- Test performance only on physical devices
- Use Xcode’s availability overrides for different device scenarios
The FoundationModels framework makes AI capabilities accessible to iOS developers while maintaining Apple’s privacy-first approach. Building intelligent, responsive features is now more achievable than ever.