Section 10/171 menit

10. App Intents + Apple Intelligence

10. App Intents + Apple Intelligence

App Intents adalah pintu masuk fitur app ke Siri, Spotlight, Shortcuts, dan Apple Intelligence. Sejak iOS 18, Apple Intelligence bisa memanggil App Intent kamu sebagai tool — fitur app jadi accessible lewat natural language.

Detail lengkap di app-intents-guide.md. Highlight di sini: implikasi AI.

Pattern: App sebagai "Tool" untuk Apple Intelligence

swift
// app-intent: Expose fitur app ke Apple Intelligence
import AppIntents

struct CreateNoteIntent: AppIntent {
    static var title: LocalizedStringResource = "Buat Catatan"
    static var description: IntentDescription = "Buat catatan baru di app"
    static var openAppWhenRun: Bool = false

    @Parameter(title: "Isi Catatan")
    var content: String

    @Parameter(title: "Tag", default: [])
    var tags: [String]

    func perform() async throws -> some IntentResult {
        let note = Note(content: content, tags: tags, createdAt: .now)
        try await NoteStore.shared.save(note)
        return .result(value: note.id.uuidString)
    }
}

User sekarang bisa bilang ke Siri: "Buat catatan: ide buat aplikasi AI nanti malam" → Apple Intelligence akan mem-parse, panggil CreateNoteIntent.

Pattern: Entity Query untuk Search Lewat Apple Intelligence

swift
// app-intent: Note sebagai entity yang bisa di-query
struct Note: AppEntity {
    static var typeDisplayRepresentation: TypeDisplayRepresentation = "Catatan"
    static var defaultQuery = NoteQuery()

    let id: UUID
    @Property(title: "Judul") var title: String
    @Property(title: "Isi") var content: String
    @Property(title: "Tag") var tags: [String]

    var displayRepresentation: DisplayRepresentation {
        DisplayRepresentation(title: "\(title)")
    }
}

struct NoteQuery: EntityQuery {
    func entities(for identifiers: [Note.ID]) async throws -> [Note] {
        try await NoteStore.shared.notes(ids: identifiers)
    }

    func suggestedEntities() async throws -> [Note] {
        try await NoteStore.shared.recentNotes(limit: 10)
    }

    func entities(matching string: String) async throws -> [Note] {
        try await NoteStore.shared.search(query: string)
    }
}

Mengapa Ini AI Move

Sebelum iOS 18, App Intent berinteraksi dengan Siri lewat exact phrase matching. Sekarang, Apple Intelligence memahami niat user secara semantik dan memilih intent yang tepat dari semua intent yang kamu expose. Kamu menulis intent — Apple Intelligence menjadikan app kamu "AI-controllable" tanpa kamu membuat AI sendiri.