Within the earlier tutorials, we explored how Basis Fashions work in iOS 26 and how one can begin constructing AI-powered options utilizing this new framework. We additionally launched the @Generable
macro, which makes it straightforward to transform generated responses into structured Swift sorts.
Now, in Half 3 of the Basis Fashions sequence, we’ll dive into one other highly effective functionality: Device Calling — a function that lets the mannequin work together along with your app’s features to carry out duties, retrieve information, or set off actions primarily based on person enter.
The on-device language mannequin isn’t able to answering each sort of query, particularly those who require real-time information, like the present climate or the most recent inventory costs. In different instances, you may want the mannequin to entry your app’s personal information to reply precisely. That’s the place Device Calling is available in that it permits the mannequin to delegate particular duties to your app’s features or exterior APIs.
On this tutorial, we’ll prolong the Ask Me Something app. Whereas the on-device mannequin can deal with basic queries, it doesn’t have entry to up-to-date details about trending motion pictures. To bridge that hole, we’ll use Device Calling to combine with the The Film Database (TMDB) API, enabling the mannequin to reply to movie-related questions utilizing reside information.

Utilizing TMDB APIs
For those who ask the Ask Me Something app about trending motion pictures, the on-device language mannequin gained’t have the reply—it merely doesn’t have entry to that sort of real-time info and should recommend checking different sources as an alternative. Let’s repair that utilizing Device Calling and the TMDB API. With this setup, every time a person asks a movie-related query, the mannequin gained’t reply with “I don’t know.” As a substitute, it should mechanically name the exterior API and return the related info immediately within the app.
Within the Xcode mission, create a MovieService
file and insert the next code:
// Mannequin for a Film
struct Film: Codable, Identifiable {
let id: Int
let title: String
let overview: String
// Coding keys to match API response
enum CodingKeys: String, CodingKey {
case id
case title
case overview
}
}
// Mannequin for the API response
struct TrendingMoviesResponse: Codable {
let outcomes: [Movie]
}
// Service class to fetch trending motion pictures
class MovieService {
// Base URL for TMDB API
non-public let baseURL = "https://api.themoviedb.org/3"
non-public let apiKey = ""
// Perform to fetch trending motion pictures utilizing async/await
func fetchTrendingMovies() async throws -> [Movie] {
// Assemble the URL for trending motion pictures
let urlString = "(baseURL)/trending/film/day?api_key=(apiKey)"
guard let url = URL(string: urlString) else {
throw URLError(.badURL)
}
// Carry out the community request
let (information, response) = strive await URLSession.shared.information(from: url)
// Verify for legitimate HTTP response
guard let httpResponse = response as? HTTPURLResponse,
(200...299).accommodates(httpResponse.statusCode) else {
throw URLError(.badServerResponse)
}
// Decode the JSON response
let decoder = JSONDecoder()
let trendingResponse = strive decoder.decode(TrendingMoviesResponse.self, from: information)
return trendingResponse.outcomes
}
}
Be sure to substitute the worth of apiKey
with your individual TMDB API key. For those who haven’t signed up but, head over to themoviedb.org and register for a free account to get your API key.
The code above is pretty simple: it calls the online API to fetch trending motion pictures, then parses the response and decodes it into an array of Film
objects.
Subsequent, we’ll use Device Calling to set off the code in MovieService
every time the person asks about trending motion pictures. To get began, create a brand new file named GetTrendingMoviesTool.swift
and add the next code:
import FoundationModels
struct GetTrendingMoviesTool: Device {
let title = "getTrendingMovies"
let description = "Get trending motion pictures and their info"
let service = MovieService()
@Generable
struct Arguments {
}
func name(arguments: Arguments) async throws -> [String] {
let motion pictures = strive await service.fetchTrendingMovies()
let formattedMovies = motion pictures.map { film in
"(film.title): (film.overview)"
}
return formattedMovies
}
}
We outline a GetTrendingMovieTool
struct that conforms to the Device
protocol — that is the usual approach to implement Device Calling within the Basis Fashions framework. The protocol requires you to specify a title
and description
for the software, together with an Arguments
struct to characterize any parameters the software may want. On this case, we don’t require further enter, so we outline an empty Arguments
construction.
For those who needed to filter trending motion pictures by style, you can outline Arguments
like this:
@Generable
struct Arguments {
@Information(description: "The style to fetch trending motion pictures")
var style: String
}
When the software is triggered by the mannequin, the name
methodology is mechanically executed. Inside it, we name the fetchTrendingMovies()
methodology from our service. After receiving the outcomes, we format them to show every film’s title and overview.
With the trending film software in place, integrating it into your app is simple. Merely open ContentView
and replace the LanguageModelSession
initialization as follows:
@State non-public var session = LanguageModelSession(instruments: [GetTrendingMoviesTool()])
You possibly can present customized instruments by passing them by means of the instruments
parameter when initializing the language mannequin session. That’s it! The language mannequin will mechanically invoke GetTrendingMoviesTool
every time it detects a query associated to trending motion pictures.
Construct and run the app, then strive asking the identical query once more. This time, the mannequin will efficiently reply with trending film info by invoking the software.

Abstract
On this tutorial, we explored software calling, a robust addition to the Basis Fashions framework in iOS 26. In contrast to primary textual content era, software calling allows the on-device language mannequin to work together along with your app’s features or entry exterior companies.
With software calling, you may considerably prolong the mannequin’s capabilities. Whether or not it’s working customized logic or fetching real-time information by means of APIs, the mannequin can now carry out context-aware duties past its built-in information.
I hope you’ve loved this tutorial sequence and really feel impressed to begin constructing smarter, AI-powered options utilizing the Basis Fashions framework.