当应用程序通过SiriKit启动时,打开另一个UIViewController

时间:2016-10-18 17:01:25

标签: ios swift siri sirikit

我正在尝试在我的iOS应用中实现SiriKit。当应用程序启动时,我想打开一个不同的视图控制器通过 Siri。

如何在我的应用程序中处理此类操作?

1 个答案:

答案 0 :(得分:0)

你可以这样做,但是,首先你必须在你的应用程序中设置SiriKit,这需要一长串的指示:https://developer.apple.com/library/prerelease/content/documentation/Intents/Conceptual/SiriIntegrationGuide/index.html#//apple_ref/doc/uid/TP40016875-CH11-SW1

还有一个样本SiriKit应用程序,Apple将其放在一起称为UnicornChat:https://developer.apple.com/library/content/samplecode/UnicornChat/Introduction/Intro.html

一旦添加了SiriKit App Extension并正确处理了Intent,您就可以使用与Intent关联的ResponseCode将其发送到您的应用程序。这将打开您的应用程序,您可以通过将以下代码添加到您的应用代表来捕获并将其发送到WhateverViewController:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    // implement to handle user activity created by Siri or by our SiriExtension

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

    // Access the storyboard and fetch an instance of the view controller
    let viewController: WhateverViewController = storyboard.instantiateViewController(withIdentifier: "WhateverViewController") as! WhateverViewController
    window?.rootViewController = viewController
    window?.makeKeyAndVisible()
}