Refresh App When it Enters Foreground.

Hello, I'm trying to build an app that has a message for the user. The message is time based, and therefore shows either morning, afternoon, or evening based on the time of day. However, the app does not automatically refresh when I open it, meaning that the "time of day" provided is not always accurate. I would like to use two State vars to store the time of day and pass my message into one, therefore allowing the variable to be refreshed, but I cannot find a way to do this without putting the state variables and function in the same part of the Struct, but I cannot use both at the same time. I have attached my code below, and I thank you for your help.

//

//  TodayView.swift

import SwiftUI

import Foundation

import UIKit

struct TodayView: View {

    @Environment(.scenePhase) var scenePhase

    

    

    //@Environment(.scenePhase) var scenePhase

    //@State var message = Message()

  

    

  

    //()

   

  

    

    @State var timeOfDay = TimeOfDay()

 

    func Message() -> String{

        

        

        

        switch month{

        case 2:

            switch day{

            case 19:

                return "Good (timeOfDay), User."

            case 20:

                return "Good (timeOfDay), User."

            case 21:

                return "Good (timeOfDay),User"

            case 22:

                return "Good (timeOfDay), User."

            default:

                return "Houston, we have an error. Please update the app to continue using it."

            }

        

    default:

      return "Please update app"

    }

        

       

    }

    

    

    

    

    

  @State var mes = Text(Message())

    

    var body: some View {

        

        

        NavigationView{

        

                

                ScrollView(.vertical, showsIndicators: false){

                    VStack {

                        Text(Message())

                            .font(Font.custom(AppFont, size: 50))

                    }

                    

                    .onChange(of: scenePhase) { newPhase in

                                    if newPhase == .active {

                                      timeOfDay = TimeOfDay()

                                    } else if newPhase == .inactive {

                                        print("Inactive")

                                    } else if newPhase == .background {

                                        print("Background")

                                    }

                                }

               }

                .navigationTitle("Daily Message")

                

               // .frame(maxHeight: infinity)

                //.offset(CGSize(width: 0, height: -250))

            Spacer()

               

            }

            

        }

    }

struct TodayView_Previews: PreviewProvider {

    static var previews: some View {

        TodayView()

    }

}

I also have this:

mport Foundation

import SwiftUI

let tghisDay = Calendar.current.dateComponents([.day, .month], from: Date())

var day = tghisDay.day!

var month = tghisDay.month!

var hour = tghisDay.hour!

struct RealMessage{

    var message:String

}

func TimeOfDay() -> String{

    

   

    

    if 11 > hour{

        return "morning"

    } else if 19 > hour{

        return "afternoon"

    } else {

        return "evening"

    }

    

        

    

       

}

Answered by AnXcodeProgrammer in 710780022

I figured out the answer. Because my message variable was from a function, it only called when the app started up - not when the view was rendered. It didn't detect when the function output changed. Instead, making it so that my function was called in the actual Text(message()) part, so it checked every time the view updated.

Accepted Answer

I figured out the answer. Because my message variable was from a function, it only called when the app started up - not when the view was rendered. It didn't detect when the function output changed. Instead, making it so that my function was called in the actual Text(message()) part, so it checked every time the view updated.

Refresh App When it Enters Foreground.
 
 
Q