Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register multiple Siri shortcut at once

I wrote some code which try to register multiple Siri shortcut at once by iterating enum values

When I run the code and open settings app, it only shows the last registered shortcut.

How should I register multiple Siri shortcut at once?

static func registerSiriShortcut(to responder: UIResponder) {

    if #available(iOS 12.0, *) {
      let cases = SiriShortcutType.allCases
      for type in cases {

        let activity = NSUserActivity(activityType: type.siriActivityType)
        activity.title = type.siriShortcutTitle
        activity.isEligibleForSearch = true
        activity.isEligibleForPrediction = true

        responder.userActivity = activity
        responder.userActivity?.becomeCurrent()
      }
    }

  }
like image 358
Bigair Avatar asked Dec 06 '25 22:12

Bigair


1 Answers

Your code seems okay to me. Generally, Settings->Siri shows only the recent registered shortcuts. If you go to Settings->Siri->All shortcuts, you will see the all names there.

As you mention in your code activity.isEligibleForSearch = true Alternatively, go to your phone search from swipe right from home and type the shortcut, you should see the shortcut item's popup too.

EDIT 1: Proof of my Code:

  1. Info.plist: You need to mention how many NSUserActivityTypes:

    <Key>NSUserActivityTypes</key>
    <array> 
    <string>com.rio.SiriShortcuts.makeGreen</string>
    <string>com.rio.SiriShortcuts.makeRed</string>
    </array>
    
  2. Enum class:

    enum SiriShortcutType {
    case makeRed
    case makeGreen
    
    var siriActivityType: String {
        switch self {
            case .makeRed:
            return "com.rio.SiriShortcuts.makeRed"
            case .makeGreen:
            return "com.rio.SiriShortcuts.makeGreen"
        }
    }
    
    var siriShortcutTitle: String {
        switch self {
        case .makeRed:
            return "Make View Red"
        case .makeGreen:
            return "Make View Green"
        }
    }
    
    var color: String {
        switch self {
        case .makeRed:
            return "red"
        case .makeGreen:
            return "green"
        }
    }
    
    static let allCases:[SiriShortcutType] = [.makeRed, .makeGreen]
    }
    
  3. Now my register method in VC (called by button action):

     func registerSiriShortcut() {
    if #available(iOS 12.0, *) {
        let cases = SiriShortcutType.allCases
        var suggestions: [INShortcut] = []
        for type in cases {
    
            let activity = NSUserActivity(activityType: type.siriActivityType)
            activity.userInfo = ["color" : type.color]
            activity.title = type.siriShortcutTitle
            activity.isEligibleForSearch = true
            activity.isEligibleForPrediction = true
            activity.persistentIdentifier = NSUserActivityPersistentIdentifier(type.siriActivityType)
            suggestions.append(INShortcut(userActivity: activity))
        }
        INVoiceShortcutCenter.shared.setShortcutSuggestions(suggestions)
    }
    }
    

Edit 1: INShortcut supports multiple Siri Shortcuts: See the updated registerSiriShortcut().

like image 125
Razib Mollick Avatar answered Dec 08 '25 14:12

Razib Mollick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!