Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI run function every time app is opened

I have a very troubling problem. I have searched for days on how to solve it. I have some code that I want to run every time the app is opened, not just when the app is launched for the first time. I've basically tried everything available. I've tried scenePhase, I've tried AppDelegate, I've tried onAppear, I've tried init and custom extensions to the View class, I've even tried simply running a function in the view, but nothing is working. I'll show my code here.

@main
struct CouponDeckApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    @Environment(\.scenePhase) private var scenePhase
    var body: some Scene {
        WindowGroup {
            AppContentView()
                
        }
    }
}

struct AppContentView: View {
    init() {
        let userDefaults = UserDefaults.standard
        
        if userDefaults.value(forKey: "hour") == nil { // 1
            userDefaults.set(9, forKey: "hour") // 2
        }
        
        // 3
        if userDefaults.value(forKey: "minute") == nil {
            userDefaults.set(30, forKey: "minute")
        }
    }
    @State var currentview: String = "Main"
    
    var body: some View {
        
        Group {
            switch currentview {
            case "Main":
                MainView(currentview: $currentview)
                    
            case "Form":
                FormView(currentview: $currentview)
            case "Settings":
                SettingsView(currentview: $currentview)
            default:
                if currentview.contains("Coupon") {
                    CouponView(currentview: $currentview)
                }
                else {
                    EditView(currentview: $currentview)
                }
                
            }
            
        }
    }
}

//MainView(), CouponView(), FormView(), etc.

I'm starting to suspect that the problem is with the switch statement in AppContentView that allows you to move between the different views.

Does anyone know: A. Why this is happening, B. How to fix it, or C. Another alternative?

Thanks in advance!

P.S. I'm running my code on the simulator.

like image 980
Kunal Katiyar Avatar asked Nov 19 '25 01:11

Kunal Katiyar


2 Answers

Here is a very simple way, using native scenePhase, I did not make it more complicated. You can use Preference method as well for better result! But onChange is good enough for this example:

 struct ContentView: View {
    
    @Environment(\.scenePhase) var scenePhase
    
    var body: some View {
        
        Text("Welcome to my App!")
            .onAppear() { customFunction() }
            .onChange(of: scenePhase) { newPhase in
                
                if newPhase == .active {
                    customFunction()
                }
                
            }
        
    }
}


func customFunction() {
    print("App is opened!")
}
like image 50
ios coder Avatar answered Nov 21 '25 10:11

ios coder


The simple problem was that it doesn't work when you close out of the app. I realized if you just exit the app but don't completely close out of it, it works just fine.

I also learned about the NotificationCenter's applications to this. By triggering a response when UIApplication sends out the willEnterForegroundNotification by using the onReceive method, you can trigger a response that way.

like image 20
Kunal Katiyar Avatar answered Nov 21 '25 09:11

Kunal Katiyar