What you’ll learn
By the end of this guide, you’ll understand:- How to integrate the LeapSDK into your iOS project
- How to load and run AI models locally on an iPhone or iPad
- How to implement real-time streaming text generation
- How to build a clean, three-layer SwiftUI architecture for AI apps
Understanding the architecture
The LeapSlogan app has a clean, three-layer architecture:Prerequisites
You will need:- Xcode 15.0+ with Swift 5.9 or later
- iOS 15.0+ deployment target
- A physical iOS device (iPhone or iPad) for best performance
- The iOS Simulator works but will be significantly slower
- Basic familiarity with SwiftUI and Swift’s async/await syntax
Setup
Create a new Xcode project
- Open Xcode and create a new iOS App
- Choose SwiftUI for the interface
- Set minimum deployment target to iOS 15.0
Add LeapSDK via Swift Package Manager
- In Xcode, go to File → Add Package Dependencies
- Enter the repository URL:
- Select the latest version (0.6.0 or newer)
- Add both products to your target:
- ✅
LeapSDK - ✅
LeapSDKTypes
- ✅
Download a model bundle
- Visit the Leap Model Library
- Download a small model like LFM2-350M (great for mobile, ~500MB)
- Download the
.bundlefile for your chosen model - Drag the
.bundlefile into your Xcode project - ✅ Make sure “Add to target” is checked
Building the ViewModel
The ViewModel manages the model lifecycle and handles generation. Create a new Swift file calledSloganViewModel.swift:
Understanding the streaming API
ThegenerateResponse() method returns an AsyncStream that emits three types of events:
.chunk(text): Each piece of generated text - makes the UI feel responsive.reasoningChunk(reasoning): Some models show their “thinking” process.complete(usage, info): Final event with token usage and performance metrics
Model loading is async and can take 1-5 seconds. In production apps, show a nice loading screen during this time.
Building the user interface
The UI provides a beautiful, interactive experience. Key features:- Progressive disclosure: Loading screen → Main interface
- Clear visual feedback: Loading states, disabled states, animations
- Helpful instructions: Users understand what to do immediately
- Polished details: Gradient background, shadows, rounded corners
- Copy functionality: Users can easily copy the generated slogan
Troubleshooting
Model bundle not found
Model bundle not found
Solution:
- Check that
.bundlefile is in Xcode project - Verify “Target Membership” is checked
- Ensure bundle name in code matches actual filename
Failed to load model
Failed to load model
Solution:
- Test on a physical device (Simulator is unreliable)
- Ensure iOS version is 15.0+
- Check device has enough free storage (~2-3x model size)
- Try a smaller model first
Slow generation speed
Slow generation speed
Solution:
- Use a physical device (10-100x faster than Simulator)
- Choose a smaller model (350M-1B)
- Lower
maxTokensin GenerationOptions - Reduce temperature for faster but less creative output
App crashes on launch
App crashes on launch
Solution:
- Ensure both
LeapSDKandLeapSDKTypesare added - Check frameworks are set to “Embed & Sign”
- Clean build folder (Cmd+Shift+K)
- Restart Xcode
Next steps
Expand your skills with these projects:- LeapChat: Build a full chat interface with history - check out the LeapChatExample
- Add structured output: Use
@Generatablemacros to generate JSON data structures - Implement function calling: Let AI call your functions for weather lookup, calculations, database queries - see the Function Calling Guide