Last active
July 9, 2017 00:26
-
-
Save seckincengiz/93dc067c6bcf483a858bb9071bd06469 to your computer and use it in GitHub Desktop.
Check App State (Swift 3)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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