Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

2021 SwiftUI & UI FrameworksApp Services

WWDC21 · 19 min · SwiftUI & UI Frameworks / App Services

Add intelligence to your widgets

Discover how to you can add intelligence to your widgets in Smart Stacks. We’ll show you how to use the new Widget Suggestions API in tandem with Smart Rotate to create more valuable widget experiences for people throughout the day. Whether you inform the system of new, timely information or teach the system to learn common patterns, adopting these APIs can help people discover your widget and allows you to influence how the system surfaces content from your app around system spaces.

Watch at developer.apple.com ↗

Transcript all transcripts

Code shown on screen · 3 snippets

Donate INRelevantShortcuts for Widget Suggestions swift · at 9:14 ↗
// Donate INRelevantShortcut for Widget Suggestions in app
// User has just made a purchase

var relevantShortcuts: [INRelevantShortcut] = []

let intent = ViewRecentPurchasesIntent()
intent.card = Card(identifier: card.identifier)
intent.category = .all

if let shortcut = INShortcut(intent: intent) {
    let relevantShortcut = INRelevantShortcut(shortcut: shortcut)
    relevantShortcut.shortcutRole = .information
    relevantShortcut.widgetKind =CardRecentPurchasesWidgetlet dateProvider = INDateRelevanceProvider(start: Date(), 
                                               end: Date(timeIntervalSinceNow: 1800))
    relevantShortcut.relevanceProviders = [dateProvider]

    relevantShortcuts.append(relevantShortcut)
}

INRelevantShortcutStore.default.setRelevantShortcuts(relevantShortcuts) { (error) in
    if let error = error {
        print("Failed to set relevant shortcuts. \(error))")
    } else {
        print("Relevant shortcuts set.")
    }
}
Adopting TimelineEntryRelevance for Smart Rotate swift · at 12:35 ↗
// Appending TimelineEntryRelevance to a TimelineEntry in widget extension for Smart Rotate

struct CardRecentPurchasesEntry: TimelineEntry {
    let date: Date
    let relevance: TimelineEntryRelevance?
    let card: IntentCard?
    let category: PurchaseCategory
}

let relevance = TimelineEntryRelevance(score: 16.29, duration: 1800)
let entry = CardRecentPurchasesEntry(date: Date(), relevance: relevance, card: card,
                                     category: category)
Donate INIntents through INInteraction for Widget Suggestions and Smart Rotations swift · at 17:01 ↗
// Donate INIntent in a card's purchases list in the app

.onAppear {
    let intent = ViewRecentPurchasesIntent()
    intent.card = Card(identifier: card.id.uuidString, displayString: card.name)
    intent.category = .all

    let interaction = INInteraction(intent: intent, response: nil)
    interaction.donate { error in
        if let error = error {
            print(error.localizedDescription)
        }
    }
}

Resources