On-device AI on iOS, the practical parts
Six guides from building two iOS apps that run language models on the iPhone: llama.cpp in Swift, JSON that always parses, Apple Intelligence with a fallback, the model download, and a fair free trial. Every snippet works on its own; no purchase needed.
Run an LLM locally on iPhone with Swift and llama.cpp
Add llama.cpp to an iOS app with Swift Package Manager, load a GGUF model on Metal, apply its chat template and generate text on the iPhone, without a server.
Force valid JSON from a local LLM with a GBNF grammar in Swift
Constrain llama.cpp with a GBNF grammar so a small on-device model can only write JSON of your shape. Grammar walkthrough, a Swift generator and the caveats.
Apple Foundation Models with a fallback for iPhones without Apple Intelligence
Use Apple's Foundation Models where Apple Intelligence exists, fall back to a local GGUF model or plain rules elsewhere, with one DynamicGenerationSchema.
llama_decode crash with long prompts: GGML_ASSERT n_tokens_all <= n_batch, explained and fixed
Why llama.cpp aborts with GGML_ASSERT(n_tokens_all <= cparams.n_batch) on long prompts, and three fixes in Swift: n_batch = n_ctx, chunking, a length check.
Download a 2 GB model in the background on iOS with URLSession, mirrors and SHA-256
A background URLSession download for large GGUF model files: survives app suspension, reattaches on relaunch, falls back to mirrors and verifies SHA-256.
A free trial that survives reinstalling: Keychain and StoreKit 2
Give people a few free uses, keep the counter in the Keychain so reinstalling doesn't reset it, and sell the unlock as a StoreKit 2 non-consumable with restore.