swiftappintentsapp-shortcut

Conditionally build AppShortcuts list


I am trying to build a list of AppIntents to use with App Shortcuts, using AppShortcutsProvider. I built 2-3 shortcuts this way and it works fine, but I want to add an intent that is based on ForegroundContinuableIntent, which is an iOS16.4 API, so I want to conditionally add that to the shortcuts list, but it doesn't work.

I tried something like this:

@available(iOS 16.0, *)
struct CJAppShortcutsList: AppShortcutsProvider {
    
    @AppShortcutsBuilder
    static var appShortcuts: [AppShortcut] {
        AppShortcut(
            intent: CJAddContact(),
            phrases: [
                "Add Contact to \(.applicationName)",
                "Create Contact in \(.applicationName)",
            ],
            shortTitle: "Create New Contact",
            systemImageName: "person.crop.circle.fill.badge.plus"
        )
        if #available(iOS 16.4, *) {
            AppShortcut(intent: CJOpenContactIntent(),
                        phrases: ["Open Contact in \(.applicationName)", "Show Contact in \(.applicationName)"],
                        shortTitle: "Open Contact",
                        systemImageName: "person.crop.circle")
            
        }
    }
}

But I get a build error:

Ambiguous use of 'buildOptional'

Is there a way to conditionally build the AppShortcuts list?


Solution

  • It works from iOS 17.4. Unfortunately there is no way to support it on older versions.

    @available(iOS 17.4, *)
    struct CJAppShortcutsList: AppShortcutsProvider {
       ...
    }
    

    Pay attention that increasing supported version means that for people with older OS versions shortcuts will disappear from Shortcuts app