Reputation: 6810
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?
Upvotes: 1
Views: 1689
Reputation: 2859
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")
}
Upvotes: 2