如何为SiriKit ios 12添加短语?

时间:2018-07-19 18:15:06

标签: swift xcode sirikit ios12

一段时间以来,我一直在尝试将Siri短语添加到我的应用中。我可以在Siri的“设置”中让Siri为用户推荐快捷方式,但是我如何允许用户在安装应用程序后立即与Siri对话?以下代码应该允许用户通过对Siri说“查看帐户”来打开“帐户页面”。

视图控制器:

import UIKit
import Intents
import os.log
import IntentsUI

// Detect the user's activity (accessing the account page)
let viewPageActivityType = "com.ibkr.sirikittest.viewAcct"

let viewPageActivity: NSUserActivity = {
    let userActivity = NSUserActivity(activityType: viewPageActivityType)
    userActivity.title = "View Account"
    userActivity.suggestedInvocationPhrase = "View Account"
    userActivity.isEligibleForSearch = true
    userActivity.isEligibleForPrediction = true
    return userActivity
}()

class AccountPage: UIViewController {

    // Once view loads, donate intent to view account page
    let intent = ViewAccountIntent()

    override func viewDidLoad() {
        let interaction = INInteraction(intent: intent, response: nil)
        interaction.donate { error in
            print("Donated Account Intent")
        super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    }
}

AppDelegate(仅相关代码):

} else if let intent = userActivity.interaction?.intent as? ViewAccountIntent {

        let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewController : UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "AccountPage") as UIViewController
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = initialViewController
        self.window?.makeKeyAndVisible()

        return true
    }
    return false

0 个答案:

没有答案