iOS 18 App Intents while supporting iOS 17

iOS 18 App Intents while supporting iOS 17

Hello,

I have an existing app that supports iOS 17. I already have three App Intents but would like to add some of the new iOS 18 app intents like ShowInAppSearchResultsIntent.

However, I am having a hard time using #available or @available to limit this ShowInAppSearchResultsIntent to iOS 18 only while still supporting iOS 17.

Obviously, the ShowInAppSearchResultsIntent needs to use @AssistantIntent which is iOS 18 only, so I mark that struct as @available(iOS 18, *). That works as expected. It is when I need to add this "SearchSnippetIntent" intent to the AppShortcutsProvider, that I begin to have trouble doing. See code below:

struct SnippetsShortcutsAppShortcutsProvider: AppShortcutsProvider {
    
    @AppShortcutsBuilder
    static var appShortcuts: [AppShortcut] {
        //iOS 17+
        AppShortcut(intent: SnippetsNewSnippetShortcutsAppIntent(), phrases: [
            "Create a New Snippet in \(.applicationName) Studio",
        ], shortTitle: "New Snippet", systemImageName: "rectangle.fill.on.rectangle.angled.fill")
        
        AppShortcut(intent: SnippetsNewLanguageShortcutsAppIntent(), phrases: [
            "Create a New Language in \(.applicationName) Studio",
        ], shortTitle: "New Language", systemImageName: "curlybraces")
        
        AppShortcut(intent: SnippetsNewTagShortcutsAppIntent(), phrases: [
            "Create a New Tag in \(.applicationName) Studio",
        ], shortTitle: "New Tag", systemImageName: "tag.fill")
        
        //iOS 18 Only
        AppShortcut(intent: SearchSnippetIntent(), phrases: [
            "Search \(.applicationName) Studio",
            "Search \(.applicationName)"
        ], shortTitle: "Search", systemImageName: "magnifyingglass")
    }
    
    let shortcutTileColor: ShortcutTileColor = .blue
}

The iOS 18 Only AppShortcut shows the following error but none of the options seem to work. Maybe I am going about it the wrong way.

'SearchSnippetIntent' is only available in iOS 18 or newer

Add 'if #available' version check

Add @available attribute to enclosing static property

Add @available attribute to enclosing struct

Thanks in advance for your help.

Are you sure this doesn't work? (not actual code)

if(#available iOS 18) {
  AppShortcut(intent: SearchSnippetIntent(), phrases: [
              "Search \(.applicationName) Studio",
              "Search \(.applicationName)"
          ], shortTitle: "Search", systemImageName: "magnifyingglass")
  }
}

Have you tried something like: (not actual code)

@available iOS 18
struct SnippetsShortcutsAppShortcutsProvider: AppShortcutsProvider {
  // List all four items here
}

@available iOS 17
struct SnippetsShortcutsAppShortcutsProvider: AppShortcutsProvider {
  // List just the three iOS 17 ones here
}
iOS 18 App Intents while supporting iOS 17
 
 
Q