Skip to content

Instantly share code, notes, and snippets.

@seckincengiz
Last active July 9, 2017 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seckincengiz/93dc067c6bcf483a858bb9071bd06469 to your computer and use it in GitHub Desktop.
Save seckincengiz/93dc067c6bcf483a858bb9071bd06469 to your computer and use it in GitHub Desktop.
Check App State (Swift 3)
override func viewDidLoad() {
super.viewDidLoad()
//Notify the selector function when UIApplicationWillResignActive
NotificationCenter.default.addObserver(self, selector: #selector(appMovedToBackground), name: .UIApplicationWillResignActive, object: nil)
}
func appMovedToBackground() {
//Optional Delay before doing any action
//Waits 5 sec before checking app state
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
self.checkApplicationState()
}
print("App is now running in Background")
}
func checkApplicationState(){
let state = UIApplication.shared.applicationState
if state == .background {
//Do Someting
//Like
performSegue(withIdentifier: "BackToMaster", sender: nil)
}
else if state == .active {
print("Active")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment