从 AppDelegate 执行 segue
Perform segue from AppDelegate
当用户将应用程序留在后台超过三分钟时,我需要从 AppDelegate 执行 segue。
这是我的代码:
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
let preferences = UserDefaults.standard
let time_in = preferences.object(forKey: "time_background") as! Date
let timenow = Date().addingTimeInterval(-3*60) as Date
if time_in <= timenow {
print("timeout")
//transition to the login
}
print(preferences.object(forKey: "session_time") ?? "test")
}
所以在 // 转换到登录 我希望 segue 将用户带回登录页面。
但我不明白如何从 AppDelegate 进行转接。
我认为尝试使用 segue 不是解决此问题的正确方法。首先,您需要获取显示的当前根视图控制器并将您的登录视图控制器推送到堆栈上。
我认为您最好的选择是将根视图控制器设置为您的登录视图控制器,然后当用户进行身份验证时,您将根视图控制器设置为您的应用程序的导航控制器。这将重置视图 hierarchy/stack 并防止内存问题,隐藏后退按钮以便用户不能跳过登录 VC 等
在我的应用程序中,我有两个自定义导航控制器。一个用于身份验证相关 vc,另一个用于主要应用程序。我只是在需要时换掉 window.rootViewController
在我的 AppDelegate 中我有
self.window = UIWindow(frame: UIScreen.main.bounds)
if authenticationManager.authenticated() {
self.initialiseMainStack()
} else {
self.initialiseAuthenticationStack()
}
self.window?.makeKeyAndVisible()
稍后再下文件..
func initialiseMainStack() {
let nav = UINavigationController()
let vc = MainViewController()
nav.setViewControllers([vc])
self.window.rootViewController = nav
}
func initialiseAuthenticationStack() {
let nav = UINavigationController()
let vc = AuthViewController()
nav.setViewControllers([vc])
self.window.rootViewController = nav
}
或者您可以保存对当前显示 viewcontroller
的引用,然后在该引用上执行 segue。
或者您可以将 window
替换为 LoginViewController
。
window?.rootViewController = loginViewController
func showLoginScreen(){
let loginVC = UIStoryboard.getMainStoryboard().instantiateViewController(withIdentifier: "LoginViewControllerIdentifier") as! LoginViewController
UIApplication.shared.delegate!.window!!.rootViewController = loginVC
}
extension UIStoryboard{
//returns storyboard from default bundle if bundle paased as nil.
public class func getMainStoryboard() -> UIStoryboard{
return UIStoryboard(name: "Main", bundle: nil)
}
}
我不知道你的应用程序结构有多难,但你可以创建一个观察者并向每个相关的实例化发送通知 viewcontroller 从那里你可以将用户导航到登录屏幕。
当用户将应用程序留在后台超过三分钟时,我需要从 AppDelegate 执行 segue。 这是我的代码:
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
let preferences = UserDefaults.standard
let time_in = preferences.object(forKey: "time_background") as! Date
let timenow = Date().addingTimeInterval(-3*60) as Date
if time_in <= timenow {
print("timeout")
//transition to the login
}
print(preferences.object(forKey: "session_time") ?? "test")
}
所以在 // 转换到登录 我希望 segue 将用户带回登录页面。
但我不明白如何从 AppDelegate 进行转接。
我认为尝试使用 segue 不是解决此问题的正确方法。首先,您需要获取显示的当前根视图控制器并将您的登录视图控制器推送到堆栈上。
我认为您最好的选择是将根视图控制器设置为您的登录视图控制器,然后当用户进行身份验证时,您将根视图控制器设置为您的应用程序的导航控制器。这将重置视图 hierarchy/stack 并防止内存问题,隐藏后退按钮以便用户不能跳过登录 VC 等
在我的应用程序中,我有两个自定义导航控制器。一个用于身份验证相关 vc,另一个用于主要应用程序。我只是在需要时换掉 window.rootViewController
在我的 AppDelegate 中我有
self.window = UIWindow(frame: UIScreen.main.bounds)
if authenticationManager.authenticated() {
self.initialiseMainStack()
} else {
self.initialiseAuthenticationStack()
}
self.window?.makeKeyAndVisible()
稍后再下文件..
func initialiseMainStack() {
let nav = UINavigationController()
let vc = MainViewController()
nav.setViewControllers([vc])
self.window.rootViewController = nav
}
func initialiseAuthenticationStack() {
let nav = UINavigationController()
let vc = AuthViewController()
nav.setViewControllers([vc])
self.window.rootViewController = nav
}
或者您可以保存对当前显示 viewcontroller
的引用,然后在该引用上执行 segue。
或者您可以将 window
替换为 LoginViewController
。
window?.rootViewController = loginViewController
func showLoginScreen(){
let loginVC = UIStoryboard.getMainStoryboard().instantiateViewController(withIdentifier: "LoginViewControllerIdentifier") as! LoginViewController
UIApplication.shared.delegate!.window!!.rootViewController = loginVC
}
extension UIStoryboard{
//returns storyboard from default bundle if bundle paased as nil.
public class func getMainStoryboard() -> UIStoryboard{
return UIStoryboard(name: "Main", bundle: nil)
}
}
我不知道你的应用程序结构有多难,但你可以创建一个观察者并向每个相关的实例化发送通知 viewcontroller 从那里你可以将用户导航到登录屏幕。