[英]Show view controller when app is returning from background (iOS 13)
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).每次我的应用程序从后台返回时,我想显示一个密码提示(相信我,这很有意义,它并不烦人;它类似于 1Password 所做的)。
So I need to show a certain ViewController every time the app enters the foreground or every 10 minutes .所以我需要在每次应用程序进入前台或每 10 分钟显示一个特定的 ViewController 。
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.
请注意,该应用程序仅为 iOS 13。 There was some change I don't understand (scenes?).
有一些我不明白的变化(场景?)。
Does someone have an idea?有人有想法吗?
Thank you.谢谢你。
You should make use of the NotificationCenter .您应该使用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.添加这行代码后,可能在您的主视图viewDidLoad()方法中,您还需要一个名为showThePasswordViewController()的 function 或任何您想要调用的名称。 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.不要被“@objc”气馁,这并不意味着你需要写在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.
还请记住,当您添加观察者时,您在#selector之后的括号中写下您的 function 名称,而在末尾 () 中没有这样的括号,如上所示。 Hope this helps, Hans.
希望这会有所帮助,汉斯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.