swift4 - I want to set a view controller as initial view controller
问题描述
When user is already logged In then the app should be open from home page and he/she should not go to the login page again.
I have tried this code it shows no error but it is not working.
let token = UserDefaults.standard.string(forKey: "token") ?? ""
if !token.isEmpty
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let firstVC =
storyboard.instantiateViewController(withIdentifier:
"initController") as! ViewController
self.window?.rootViewController = firstVC
self.window?.makeKeyAndVisible()
}
I expect that when user is already logged In then when she again open the app he/she must directly go the home page and should not go again to the login page.
解决方案
let token = UserDefaults.standard.string(forKey: "token") ?? ""
if !token.isEmpty
{
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let yourVC = mainStoryboard.instantiateViewController(withIdentifier:
"initController") as! HomeController
appDelegate.window?.rootViewController = yourVC
appDelegate.window?.makeKeyAndVisible()
super.viewDidLoad()
}
推荐阅读
- reactjs - 如何使用 Internet Explorer 11 访问本地网站?
- python - pywinauto 访问嵌套的子菜单
- hibernate - 将 Hibernate3 迁移到 Hibernate5,Formatter 类
- visual-studio-code - 带有“\”或“\\”的差异路径
- angular - 在移动设备上滚动时,角度触发器中的 ngx-slider 会不必要地触发
- jmx - 如何为咖啡因缓存激活 JMX
- python - 在 PySpark 中展开 json 列 - 架构问题 - AttributeError: 'tuple' object has no attribute 'name'
- c# - 如何停止 WPF 中的后台工作人员?
- javascript - 消除 redux 调度未按预期工作
- apache-spark - 如何使用 Spark 在 Apache Solr 上构建聚合