Returning to previously opened app

Hi everyone, I’m just starting with swift and Xcode and have a basic question. I have the following code I found online for an app that generates math addition questions. I would like to run this Math app on my iPhone just before I open the apps I use most often (let’s say mail, WhatsApp, calendar and notes) ask me a maths question and if I answer correctly, carryon with the app originally intended to be opened.

I can do the opening of the Math app before the apps I use more often with shortcuts. I would like to modify the code bellow so that if I answer correctly it “closes” itself and returns to the originally intended app. With that intention I included the “exit(0)”, but I get an error. Thanks for your help in advance! Best, Tom

struct ContentView: View {
@State private var correctAnswer = 0
@State private var choiceArray : [Int] = [0, 1, 2, 3]
@State private var firstNumber = 0
@State private var secondNumber = 0
@State private var difficulty = 1000
var body: some View {
VStack {
Text("(firstNumber) + (secondNumber)")
.font(.largeTitle)
.bold()
HStack {
ForEach(0..<2) {index in
Button {
answerIsCorrect(answer: choiceArray[index])
generateAnswers()
} label: {
AnswerButton(number: choiceArray[index])
}
}
}
HStack {
ForEach(2..<4) {index in
Button {
answerIsCorrect(answer: choiceArray[index])
generateAnswers()
} label: {
AnswerButton(number: choiceArray[index])
}
}
}
}
func answerIsCorrect(answer: Int){
if answer == correctAnswer {exit(0)}
}
}
func generateAnswers(){
firstNumber = Int.random(in: 0...(difficulty/2))
secondNumber = Int.random(in: 0...(difficulty/2))
var answerList = Int
correctAnswer = firstNumber + secondNumber
for _ in 0...2 {
answerList.append(Int.random(in: 0...difficulty))
}
answerList.append(correctAnswer)
choiceArray = answerList.shuffled()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

Replies

it “closes” itself and returns to the originally intended app.

Unfortunately that’s not a thing on iOS. Calling exit() is equivalent to just crashing, which always dumps the user back at the home screen.