2

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.

3 Answers 3

11

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!")
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

Do it in your AppDelegate's application(_:didFinishLaunchingWithOptions:).

4 Comments

OP said "every time the app is opened, not when the app is launched for the first time" though
I assumed "app is launched for the first time" means first launch after installation. Detecting first launch after install using UserDefaults is something well covered by other pages on stackoverflow.
Ah. I thought they were counting an "open" as pressing home button to go to home screen, then clicking on app.
@aheze: OP: "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."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.