2

I followed some discussions, for example this one, and tried with following suggested code:

  let state = UIApplication.shared.applicationState
    if state == .background {
        print("App in Background")
    }else if state == .active {
        print("App in Foreground or Active")
    }

But it gives me prints only in active state. I get nothing from the background state. How could I get notice if my app is in background? Where I should call this function? Has somebody any experiences with this problem?

Roman
  • 759
  • 2
  • 9
  • 23

1 Answers1

0

Since you are using SpriteKit, I assume the code to check state that you provided is being called somewhere in the didMove(to:) method of your version of GameScene.swift. If that is the case, then you are not experiencing a problem, but a desired result; the app is always in an active state when the scene has called the didMove(to:) method.

To capture each state, try implementing these three methods inside of your version of AppDelegate.swift:

Swift 4

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    print("app finished launching")
    return true
}

func applicationDidEnterBackground(_ application: UIApplication) {
    print("app in background")
}

func applicationWillEnterForeground(_ application: UIApplication) {
    print("app in foreground")
}
nhiddink
  • 235
  • 1
  • 12