I'd like to show a password prompt every time my app returns from the background (trust me, it makes sense, it's not annoying; it's similar to what 1Password does).
So I need to show a certain ViewController every time the app enters the foreground or every 10 minutes.
I tried a lot, but I don't seem to get it to work. For instance:
func applicationWillResignActive(_ application: UIApplication) {
print("called it")
}
func applicationDidBecomeActive(_ application: UIApplication) {
print("called it")
}
func applicationWillEnterForeground(_ application: UIApplication) {
print("called it")
}
These methods simply don't get called on my device. Please note that the app is iOS 13 only. There was some change I don't understand (scenes?).
Does someone have an idea?
Thank you.
You should make use of the NotificationCenter.
NotificationCenter.default.addObserver(self, selector: #selector(showThePasswordViewController), name: UIApplication.didBecomeActiveNotification, object: nil)
After adding this line of code, possibly in your main views viewDidLoad() method, you also need a function called showThePasswordViewController() or whatever you want to call it. Make sure to declare it like this in the same view as where you add the observer:
@objc showThePasswordViewController() {
//your code to present it
}
Don't be discouraged by the "@objc", it does not mean you need to write in objective-C. Also remember that when you add the observer you write your function name in the parentheses after #selector without the parentheses like this at the end () as demonstrated above. Hope this helps, Hans.