π£οΈ Siri μμ± μ μ΄ μ± λ§λ€κΈ°
β Difficulty: βββ
β±οΈ Est. Time: 2-3h
π App Frameworks
Build an app that works when you say "Add a todo" using App Intents.
β¨ App Intents?
App Intents exposes your app's features to Siri, Shortcuts, and Spotlight. Available on iOS 16+ and much simpler than the previous SiriKit.
π 첫 λ²μ§Έ Intent λ§λ€κΈ°
AddTodoIntent.swift
import AppIntents struct AddTodoIntent: AppIntent { static var title: LocalizedStringResource = "ν μΌ μΆκ°" @Parameter(title: "μ λͺ©") var todoTitle: String func perform() async throws -> some IntentResult { TodoStore.shared.add(title: todoTitle) return .result(dialog: "\(todoTitle) μΆκ° μλ£!") } }
π€ Siri μμ± λͺ λ Ή λ±λ‘
Shortcuts.swift
struct TodoShortcuts: AppShortcutsProvider { static var appShortcuts: [AppShortcut] { AppShortcut( intent: AddTodoIntent(), phrases: [ "ν μΌ μΆκ°ν΄μ€ \(.applicationName)", "\(.applicationName)μ ν μΌ μΆκ°" ], shortTitle: "ν μΌ μΆκ°", systemImageName: "plus.circle" ) } }
π‘ HIG ν
Write phrases as natural sentences β use expressions people would actually say, like "Add a todo" or "Create a new task."
π νλΌλ―Έν° λν νλ¦
νλΌλ―Έν°κ° μμΌλ©΄ Siriκ° μλμΌλ‘ λ¬Όμ΄λ΄ λλ€:
Dialog.swift
guard let title = todoTitle else { throw $todoTitle.needsValueError("μ΄λ€ ν μΌμ μΆκ°ν κΉμ?") }
π Entity μ μ
Define todo items as Entities so Siri can suggest and search tasks:
TodoEntity.swift
struct TodoEntity: AppEntity { static var typeDisplayRepresentation: TypeDisplayRepresentation { "ν μΌ" } var id: UUID var title: String var isCompleted: Bool var displayRepresentation: DisplayRepresentation { DisplayRepresentation(title: "\(title)") } // Spotlight κ²μ μ§μ static var defaultQuery = TodoEntityQuery() }
π EntityQuery ꡬν
TodoEntityQuery.swift
struct TodoEntityQuery: EntityQuery { func entities(for identifiers: [UUID]) async throws -> [TodoEntity] { TodoStore.shared.todos.filter { identifiers.contains($0.id) } } func suggestedEntities() async throws -> [TodoEntity] { // λ―Έμλ£ νλͺ© 3κ° μ μ TodoStore.shared.todos .filter { !$0.isCompleted } .prefix(3) .map(TodoEntity.init) } }
π± SwiftUI Integration
Use AppIntentsUI to customize Siri responses with SwiftUI:
AddTodoIntent+UI.swift
import AppIntentsUI extension AddTodoIntent { @MainActor func perform() async throws -> some IntentResult & ProvidesDialog & ShowsSnippetView { let todo = TodoStore.shared.add(title: todoTitle) return .result( dialog: "\(todoTitle) μΆκ°νμ΄μ!", view: TodoSnippetView(todo: todo) ) } } struct TodoSnippetView: View { let todo: TodoEntity var body: some View { HStack { Image(systemName: "checkmark.circle") Text(todo.title) } .padding() } }
π‘ Testing λ°©λ²
Run in Xcode and ask Siri "Add a todo [app name]." In the simulator, test via Hardware β Siri.