Zum Inhalt springen

WWDC 2025 – Bring on-device AI to your app with Foundation Models framework

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

  1. Use Playgrounds for efficient prompt iteration
  2. Structure your output with guided generation
  3. Plan for availability – handle different device states gracefully
  4. Stream responses for better perceived performance
  5. Optimize strategically with prewarming and schema settings
  6. 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.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert