SpotlightSearchTool is a new Foundation Models tool that lets a language model directly query your app's Core Spotlight index, enabling conversational, context-aware search over your app's own donated content. Developers can drop it into any LanguageModelSession to get AI-generated answers grounded in app data.
• Enables natural-language Q&A over app-specific content without sending data to an external server — the model reasons over your Core Spotlight index on-device.
• Zero boilerplate: one line of code adds the tool to an existing LanguageModelSession, and the model automatically decides when to invoke it.
• Supports advanced customization — GuidanceProfiles, contact resolvers, and custom pipeline stages let you tune relevance and computation for complex queries.
Demonstrates adding SpotlightSearchTool to a LanguageModelSession so a user can ask natural-language questions about hiking trails their app has indexed in Core Spotlight, and shows how to recover full item metadata via the index delegate.
import SwiftUI
import CoreSpotlight
import FoundationModels
// MARK: - Index Delegate (supplies full item metadata to the model)
final class TrailIndexDelegate: NSObject, CSSearchableIndexDelegate {
func searchableIndex(_ searchableIndex: CSSearchableIndex,
reindexAllSearchableItemsWithAcknowledgementHandler acknowledgementHandler: @escaping () -> Void) {
acknowledgementHandler()
}
func searchableIndex(_ searchableIndex: CSSearchableIndex,
reindexSearchableItemsWithIdentifiers identifiers: [String],
acknowledgementHandler: @escaping () -> Void) {
acknowledgementHandler()
}
// New iOS 27 method: called by SpotlightSearchTool to recover full item data
func searchableItems(forIdentifiers identifiers: [String]) async -> [CSSearchableItem] {
return identifiers.map { id in
let attrs = CSSearchableItemAttributeSet(contentType: .text)
attrs.title = "Sample Trail"
attrs.contentDescription = "A beautiful coastal hike with ocean views."
attrs.namedLocation = "Point Reyes, CA"
// Add any extra metadata not stored compactly in the index
attrs.completionDate = Date()
attrs.distance = 12.4
return CSSearchableItem(uniqueIdentifier: id,
domainIdentifier: "com.example.trails",
attributeSet: attrs)
}
}
}
// MARK: - Trail Search View
struct TrailAssistantView: View {
@State private var question: String = ""
@State private var answer: String = ""
@State private var isLoading: Bool = false
private let indexDelegate = TrailIndexDelegate()
var body: some View {
NavigationStack {
VStack(spacing: 16) {
TextField("Ask about your hikes…", text: $question)
.textFieldStyle(.roundedBorder)
.padding(.horizontal)
Button("Ask") {
Task { await askModel() }
}
.buttonStyle(.borderedProminent)
.disabled(question.isEmpty || isLoading)
if isLoading {
ProgressView("Thinking…")
}
ScrollView {
Text(answer)
.padding()
.frame(maxWidth: .infinity, alignment: .leading)
}
}
.navigationTitle("Trail Journal")
}
.onAppear { setupIndex() }
}
private func setupIndex() {
// Register delegate so SpotlightSearchTool can recover full metadata
CSSearchableIndex.default().indexDelegate = indexDelegate
}
private func askModel() async {
isLoading = true
answer = ""
defer { isLoading = false }
// Create the SpotlightSearchTool scoped to this app's bundle
let searchTool = SpotlightSearchTool()
// Build a session with the on-device model and attach the tool
let session = LanguageModelSession(
model: SystemLanguageModel.default,
tools: [searchTool]
)
do {
let response = try await session.respond(to: question)
answer = response.content
} catch {
answer = "Error: \(error.localizedDescription)"
}
}
}Foundation Models is a new Apple framework introduced in iOS 27 that gives developers on-device access to the same Apple Intelligence language model powering system features, enabling text generation, structured output, and tool-calling entirely on-device without a network connection.
Visual Intelligence brings iOS 17's Visual Look Up capabilities to a new developer-facing API surface in iOS 27, letting apps pipe live camera frames or static images through on-device scene understanding to extract subjects, text, barcodes, and rich semantic labels without any cloud round-trip.
iOS 27 opens the Foundation Models framework to third-party LLM providers via a new public LanguageModel protocol, allowing anyone to integrate server-based or local models (e.g. Claude, Gemini, CoreAI, MLX) using the same API as Apple's on-device system model.
In-depth guide
iOS 27 On-Device AI & Apple Intelligence →Some metadata stored in Spotlight (e.g. text content, HTML) is stored in a compact, non-recoverable form — implement the new searchableItems(forIdentifiers:) delegate method to supply full CSSearchableItem data to the model. The model may call SpotlightSearchTool multiple times per response; use queryToken to track batches and refresh UI correctly.
Requires Apple Intelligence-capable device for on-device SystemLanguageModel; SpotlightSearchTool itself works with any LanguageModel but responses depend on model context size.
iOS 27 opens the Foundation Models framework to third-party LLM providers via a new public LanguageModel protocol, enabling anyone to integrate custom, server-based, or open-source models using the same Swift API as Apple's on-device system model.