How to open/trigger a view controller with Siri?

I'm studying SiriKit and I have a big problem. I build an app and sometimes he works, sometimes he doesn't works. I want open a specific controller without say "Start apple exercise" or something like that. I user workout because it's the most simple and I wanna know if I can only say "Open profile via MyApp". Is that possible?

Here is my code:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    guard let intent = userActivity.interaction?.intent as? INStartWorkoutIntent else {
        print("AppDelegate: Start Workout Intent - FALSE")
        return false
    }
    print("AppDelegate: Start Workout Intent - TRUE")
    print("INTENT: ", intent)

    self.window = UIWindow(frame: UIScreen.main.bounds)

    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    guard let spokenPhrase = intent.workoutName?.spokenPhrase else {
        return false
    }

    switch spokenPhrase {
    case "test":
        let vc = storyboard.instantiateViewController(withIdentifier: "firstVC")

        self.window?.rootViewController = vc
        self.window?.makeKeyAndVisible()


    case "apple":
        let vc = storyboard.instantiateViewController(withIdentifier: "secondVC")

        self.window?.rootViewController = vc
        self.window?.makeKeyAndVisible()

    default:
        break
    }

    return true
}

Upvotes: 1

Views: 937

Answers (1)

David Pasztor
David Pasztor

Reputation: 54745

No, that is not possible. SiriKit can only be used in really specific scenarios. Due to the fact that Siri works with natural language processing, it needs to "listen to" some specific words in a text to be able to decode those into Swift objects. With each Intent there are some specific keywords that need to be said, otherwise Siri won't recognise the user input as a specific Intent.

Even you would "hack" your app into doing something completely irrelevant to workouts and opening it from Siri using workout intents, your app would most probably get rejected from the AppStore.

Upvotes: 2

Related Questions