I need to build a quiz type application using Siri. Here, let us consider, my application have only one question along with multiple choices as answers. Now with the Siri voice commands, I need to load that question along with choices onto the Label/ textview. Once that question gets loaded onto the Lable/textview, this question should be spoken by Siri. Now the user can choose his/her answer either a or b or c or d using their voice command. Then Siri should validate the user's input with the correct answer. If its correct, Siri should say”correct answer”. Otherwise It should say “wrong answer. The correct answer is and so on..”
I have implemented half part of my requirements, Using Siri shortcuts, I can able to load the question onto a Label and Siri can speak that question.
for your reference, please find my code below.
In ViewController.swift
file I have implemented below code,
Public fund createShortcutForloadingQuestion(){
let userAct = NSUserActivity(activityType: "com.organization.QuizSpeakingApp.loadQuestion")
userAct.title = "get the text from document"
userAct.userInfo = ["question" : "what is the capital of India? \n a. Kolkata \n b. Mumbai, \n c. Bengaluru,\n d. New Delhi"]
userAct.isEligibleForSearch = true
userAct.isEligibleForPrediction = true
userAct.persistentIdentifier = NSUserActivityPersistentIdentifier("com.organization.QuizSpeakingApp.loadQuestion")
textLbl.userActivity = userAct
userAct.becomeCurrent()
displayTextOnLabel()
}
public func displayTextOnLabel() {
textLbl.text = “what is the capital of India? \n a. Kolkata \n b. Mumbai, \n c. Bengaluru,\n d. New Delhi.”
}
To speak the loaded question I have implemented the code like below,
public func speakTheQuestion(){
let speechUtterance = AVSpeechUtterance(string: textLbl.text ?? "Label Doesnt have any text")
speechUtterance.rate = 0.5
speechUtterance.pitchMultiplier = 1.0
speechUtterance.volume = 1.0
speechUtterance.postUtteranceDelay = 0.005
speechSynthesizer.speak(speechUtterance)
}
Now to create a shortcut for loading the question and to speak the loaded question,
I have implemented the below method in AppDelegate.swift
.
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
let viewController = window?.rootViewController as! ViewController
viewController.displayTextOnLabel()
viewController.speakTheQuestion()
return true
}
With the above implementation I can able to load the question and spoken it by Siri.
Now, Anyone can help me how to take user answer with voice command, and how to validate their answer with the correct answer and how to give update about his answer with Siri?
Thanks in andvance.