简体   繁体   English

当应用程序从后台返回时显示视图 controller (iOS 13)

[英]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.

相关问题 iOS:如果应用来自背景,请从顶视图控制器重新初始化该应用 - iOS: Reinitialize the the app from top view controller if it is coming from background 在 iOS 13+ 中,即使使用 UIModalPresentationStyle.FullScreen 从后台返回时也不会调用 iOS viewWillAppear - iOS viewWillAppear not being called when returning from background, even with UIModalPresentationStyle.FullScreen, in iOS 13+ 当app从后台返回时,Xamarin iOS呈现不同的视图控制器 - Xamarin iOS present a different view controller when app returns from background 当应用程序从 IOS Swift 应用程序的后台变为活动时,如何呈现特定视图 Controller - How to Present a particular View Controller when the application Becomes active from background on IOS Swift App 视图控制器响应iOS 12中的应用程序委托通知,但不响应iOS 13中的应用委托通知 - View controller responds to app delegate notifications in iOS 12 but not in iOS 13 如何从根控制器ios应用显示视图 - how to show view from root controller ios app 更改 Storybord 视图 Controller 时 iOS 13 出现黑屏 - Black Screen in iOS 13 when change Storybord View Controller 当应用程序在后台时,是否可以从 iOS 中的导航 controller 中弹出视图 controller? - Is it possible to pop view controller from navigation controller in iOS while the app is in background? iOS 13 应用程序在后台使用 sinch 时调用包崩溃 - iOS 13 callkit crash when app in background using sinch 应用程序在后台运行时重置约束 - iOS 13 - Constraints resets when app is going in background - iOS 13
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM