简体   繁体   中英

iOS siri shortcuts Swift

I am learning swift currently for our radio station, and I have noticed that a rival station as been able to set up a siri shortcut that allows people to say "Hey Siri play (station name)" and Siri will open the app and start playing said station.

I am wondering how that could be done with Swift?

You can do it with the help of Siri's intent . You will have to create a Siri intent definition file using Xcode.

Sample Intent Definition for PlayGame:

在此处输入图片说明

In the above example, I have created simple intent for play games. I have added category as Do , There are many categories you can choose from. You can also pass the parameter through intent, just like I have used game name you can add radio station name there.

You can also add this particular intent for the Siri suggestion feature.

Handling Siri Intent event:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {

    if userActivity.activityType == "PlayGameIntent" {
        print(userActivity.userInfo ?? "")
    }

    return true
}

Note: You will have to donate this intent to the iOS to handle, you can do it by following way:

let intent = PlayGameIntent()
intent.gameName = "PUBG"

let interaction = INInteraction(intent: intent, response: nil)
interaction.donate { (error) in
    print(error ?? "error")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM