Implementing AI-Powered Text Autocomplete for Typing in Mobile Applications
Built-in keyboard autocomplete on iOS and Android predicts the next word from frequency n-grams. AI autocomplete understands context: knows the user is writing a business email, sees first two paragraphs, suggests complete phrases, not single words.
The gap between concept and working implementation lies in UX and performance details.
When to offer suggestions
Most underestimated part: the trigger. Suggestion shouldn't appear per character.
Working heuristic: offer autocomplete if user typed 3+ words on current line and paused > 600 ms, or hit space at end of incomplete sentence.
// iOS—autocomplete trigger
private var autocompleteTask: Task<Void, Never>?
func textDidChange(_ textView: UITextView) {
autocompleteTask?.cancel()
let text = textView.text ?? ""
let cursorPosition = textView.selectedRange.location
let textBeforeCursor = String(text.prefix(cursorPosition))
// Don't suggest in middle of word
guard textBeforeCursor.last == " " || textBeforeCursor.last == "\n" else {
hideAutocomplete()
return
}
// Minimum 15 characters context
guard textBeforeCursor.trimmingCharacters(in: .whitespaces).count > 15 else { return }
autocompleteTask = Task {
try? await Task.sleep(nanoseconds: 600_000_000) // 600ms debounce
guard !Task.isCancelled else { return }
await fetchAutocomplete(context: textBeforeCursor)
}
}
Request to model and response parsing
For autocomplete use completion mode, not chat. gpt-4o-mini with max_tokens: 30 and temperature: 0.3—fast and predictable.
struct AutocompleteRequest: Encodable {
let model = "gpt-4o-mini"
let messages: [ChatMessage]
let maxTokens = 30
let temperature = 0.3
let stop = ["\n", "."] // stop at end of sentence
}
func buildPrompt(context: String) -> [ChatMessage] {
[
ChatMessage(role: "system", content: "Complete the text naturally. Continue from where it ends. Output only the continuation, no commentary."),
ChatMessage(role: "user", content: context)
]
}
Stop tokens \n and . matter. Without them, model generates several sentences; we need one continuation.
Alternative for on-device: CreateML Text Classifier doesn't fit; need generative model. iOS 18+ has Foundation Models framework with on-device LLM (Apple Intelligence). Android has Gemini Nano via Google AI Edge SDK:
// Android—Gemini Nano on-device (requires device support)
val generativeModel = GenerativeModel(
modelName = "gemini-nano",
generationConfig = generationConfig {
maxOutputTokens = 30
temperature = 0.3f
stopSequences = listOf(".", "\n")
}
)
val response = generativeModel.generateContent(
content { text("Complete naturally: $contextText") }
)
val completion = response.text?.trim() ?: ""
Gemini Nano available on Pixel 8+ and some Samsung—not universal. For broad audience, need server fallback.
Display suggestion
Standard pattern: gray inline text after cursor. User presses Tab or swipes right—suggestion accepted. Any other input—hidden.
// Android Compose—inline suggestion
@Composable
fun TextFieldWithSuggestion(
value: String,
suggestion: String,
onValueChange: (String) -> Unit,
onAcceptSuggestion: () -> Unit
) {
val annotatedText = buildAnnotatedString {
append(value)
withStyle(SpanStyle(color = Color.Gray.copy(alpha = 0.6f))) {
append(suggestion)
}
}
BasicTextField(
value = TextFieldValue(
annotatedString = annotatedText,
selection = TextRange(value.length) // cursor after real text
),
onValueChange = { tfv ->
val newText = tfv.text.take(value.length + suggestion.length)
if (newText.startsWith(value + suggestion)) {
onAcceptSuggestion()
} else {
onValueChange(tfv.text.take(value.length))
}
},
keyboardActions = KeyboardActions(
onDone = { onAcceptSuggestion() }
)
)
}
iOS inline suggestion via UITextInput + drawText(in:) or simpler overlay label positioned via caretRect(for:).
Common issues
Suggestion flickers. Happens if new request returns faster than 200 ms and replaces previous immediately. Solution: show only if new suggestion differs from previous by > 3 characters.
Model continues deleted text. If user deleted part—context for prompt must be current version, not previous. Track textBeforeCursor sync with actual TextStorage state.
Tab intercepted by system. On Android, Tab on soft keyboard unavailable. Use custom inline key or gesture swipe-right via GestureDetector.
Timeline estimates
Basic autocomplete with server API + inline UI—5–8 days. On-device via Apple Intelligence / Gemini Nano with server fallback—2–3 weeks.







