This is what you will 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
Understanding the Architecture
Before we write code, let’s understand what we’re building. The LeapSlogan app has a clean, three-layer architecture:Environment setup
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
Step 1: 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
Step 2: Add LeapSDK via Swift Package Manager
LeapSDK is distributed as a Swift Package, making integration straightforward:- 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
- ✅
Important: Starting with version 0.5.0, you must add bothLeapSDKandLeapSDKTypesfor proper runtime linking.
Step 3: Download a Model Bundle
Now we need an AI model. LeapSDK uses model bundles - packaged files containing the model and its configuration:- Visit the Leap Model Library
- For this tutorial, 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
Step 4: Building the ViewModel
The ViewModel is the heart of our app. It manages the model lifecycle and handles generation. Let’s build it step by step.Step 4.1: Create the Basic Structure
Create a new Swift file calledSloganViewModel.swift:
@Observableis Swift’s new observation macro (iOS 17+, but works great on iOS 15 with backports)- We track four pieces of UI state: loading, generating, the slogan text, and any errors
ModelRunnerandConversationare private—these are our LeapSDK objects
Step 4.2: Implement Model Loading
Add the model loading function:- Bundle lookup: We find the model bundle in our app’s resources
- Async loading:
Leap.load()is async because loading models takes time (1-5 seconds) - Conversation creation: Every generation needs a
Conversationobject that tracks history - Error handling: We catch and display any loading failures
💡 Pro Tip: Model loading is the slowest part. In production apps, show a nice loading screen!
Step 4.3: Implement Slogan Generation
Now for the exciting part—generating slogans! Add this function:generateResponse() method returns an AsyncStream that emits three types of events:
-
.chunk(text): Each piece of generated text arrives here- This is what makes the UI feel responsive!
- Text appears word-by-word, just like ChatGPT
-
.reasoningChunk(reasoning): Some models show their “thinking”- Advanced feature for models that explain their reasoning
-
.complete(usage, info): The final event when generation finishes- Contains token usage statistics
- Includes performance metrics (tokens/second)
Step 5: Building the User Interface
Now let’s create a beautiful, interactive UI. Create or modifyContentView.swift:
- 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 Common Issues
Issue 1: "Model bundle not found"
Issue 1: "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
Issue 2: "Failed to load model"
Issue 2: "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
Issue 3: Slow generation speed
Issue 3: 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
Issue 4: App crashes on launch
Issue 4: 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
Congratulations! 🎉 You’ve built a fully functional on-device AI app. Here are some ideas to expand your skills:Immediate Next Projects
-
LeapChat: Build a full chat interface with history
- Check out the LeapChatExample
-
Add Structured Output: Use
@Generatablemacros- Generate JSON data structures
- Validate output format at compile-time
-
Implement Function Calling: Let AI call your functions
- Weather lookup, calculations, database queries
- See Function Calling Guide