Open a different UIViewController when app is laun

2019-07-15 05:50发布

I am trying to implement SiriKit in my iOS app. I want to open a different view controller when the app is launched through Siri.

How can I handle this type of operation in my app?

1条回答
聊天终结者
2楼-- · 2019-07-15 06:31

You can do this, however, first you will have to setup SiriKit in your app, which is a bit of work with a long list of instructions: https://developer.apple.com/library/prerelease/content/documentation/Intents/Conceptual/SiriIntegrationGuide/index.html#//apple_ref/doc/uid/TP40016875-CH11-SW1 .

There's also a sample SiriKit App that Apple has put together called UnicornChat: https://developer.apple.com/library/content/samplecode/UnicornChat/Introduction/Intro.html

Once you have added your SiriKit App Extension and have handled your Intent properly, you will be able to send it to your app using a ResponseCode associated with your Intent. This will open your app, and you can catch that and send it to WhateverViewController by adding the following code to your app delegate:

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()
}
查看更多
登录 后发表回答